Search in sources :

Example 6 with AccessControlList

use of com.qcloud.cos.model.AccessControlList in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSClientTest method putObjectFromLocalFile.

// 从本地上传
protected static PutObjectResult putObjectFromLocalFile(File localFile, String key, SSECustomerKey sseCKey, SSECOSKeyManagementParams params) {
    if (!judgeUserInfoValid()) {
        return null;
    }
    AccessControlList acl = new AccessControlList();
    UinGrantee uinGrantee = new UinGrantee("734000014");
    acl.grantPermission(uinGrantee, Permission.Read);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    if (sseCKey != null) {
        putObjectRequest.setSSECustomerKey(sseCKey);
    }
    if (params != null) {
        putObjectRequest.setSSECOSKeyManagementParams(params);
    }
    // putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    // putObjectRequest.setAccessControlList(acl);
    PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
    assertNotNull(putObjectResult.getRequestId());
    assertNotNull(putObjectResult.getDateStr());
    String etag = putObjectResult.getETag();
    String expectEtag = null;
    try {
        expectEtag = Md5Utils.md5Hex(localFile);
    } catch (IOException e) {
        fail(e.toString());
    }
    if (useClientEncryption) {
        assertEquals(false, expectEtag.equals(etag));
    } else {
        assertEquals(true, expectEtag.equals(etag));
    }
    return putObjectResult;
}
Also used : AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) PutObjectResult(com.qcloud.cos.model.PutObjectResult) IOException(java.io.IOException) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 7 with AccessControlList

use of com.qcloud.cos.model.AccessControlList in project cos-java-sdk-v5 by tencentyun.

the class AclTest method setGetBucketAclTest.

@Test
public void setGetBucketAclTest() {
    if (!judgeUserInfoValid()) {
        return;
    }
    AccessControlList acl = new AccessControlList();
    Owner owner = new Owner();
    owner.setId("qcs::cam::uin/2779643970:uin/2779643970");
    acl.setOwner(owner);
    String id = "qcs::cam::uin/2779643970:uin/734505014";
    UinGrantee uinGrantee = new UinGrantee(id);
    uinGrantee.setIdentifier(id);
    acl.grantPermission(uinGrantee, Permission.FullControl);
    cosclient.setBucketAcl(bucket, acl);
    AccessControlList aclGet = cosclient.getBucketAcl(bucket);
    List<Grant> grants = aclGet.getGrantsAsList();
    assertEquals(1L, grants.size());
    assertEquals(id, grants.get(0).getGrantee().getIdentifier());
    assertEquals(Permission.FullControl.toString(), grants.get(0).getPermission().toString());
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) Owner(com.qcloud.cos.model.Owner) Test(org.junit.Test)

Example 8 with AccessControlList

use of com.qcloud.cos.model.AccessControlList in project cos-java-sdk-v5 by tencentyun.

the class GeneratePresignedUrlTest method testAnonymousUrl.

@Test
public void testAnonymousUrl() throws InterruptedException, IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    AccessControlList oldAcl = cosclient.getBucketAcl(bucket);
    cosclient.setBucketAcl(bucket, CannedAccessControlList.PublicReadWrite);
    Thread.sleep(5000L);
    COSCredentials cred = new AnonymousCOSCredentials();
    ClientConfig anoyClientConfig = new ClientConfig(new Region(region));
    COSClient anoyCOSClient = new COSClient(cred, anoyClientConfig);
    String key = "ut/generate_url_test_upload.txt";
    File localFile = buildTestFile(1024);
    File downLoadFile = new File(localFile.getAbsolutePath() + ".down");
    try {
        Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
        URL putUrl = anoyCOSClient.generatePresignedUrl(bucket, key, expirationTime, HttpMethodName.PUT, new HashMap<String, String>(), new HashMap<String, String>());
        URL getUrl = anoyCOSClient.generatePresignedUrl(bucket, key, expirationTime, HttpMethodName.GET, new HashMap<String, String>(), new HashMap<String, String>());
        URL delUrl = anoyCOSClient.generatePresignedUrl(bucket, key, expirationTime, HttpMethodName.DELETE, new HashMap<String, String>(), new HashMap<String, String>());
        assertTrue(putUrl.toString().startsWith("https://"));
        assertTrue(getUrl.toString().startsWith("https://"));
        assertTrue(delUrl.toString().startsWith("https://"));
        assertFalse(putUrl.toString().contains("sign="));
        assertFalse(getUrl.toString().contains("sign="));
        assertFalse(delUrl.toString().contains("sign="));
        testPutFileWithUrl(putUrl, localFile);
        headSimpleObject(key, localFile.length(), Md5Utils.md5Hex(localFile));
        testGetFileWithUrl(getUrl, downLoadFile);
        assertEquals(localFile.length(), downLoadFile.length());
        assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downLoadFile));
        testDelFileWithUrl(delUrl);
        assertFalse(cosclient.doesObjectExist(bucket, key));
    } finally {
        clearObject(key);
        assertTrue(localFile.delete());
        assertTrue(downLoadFile.delete());
        anoyCOSClient.shutdown();
        cosclient.setBucketAcl(bucket, oldAcl);
        Thread.sleep(5000L);
    }
}
Also used : AccessControlList(com.qcloud.cos.model.AccessControlList) CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) COSCredentials(com.qcloud.cos.auth.COSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) File(java.io.File) Date(java.util.Date) URL(java.net.URL) Test(org.junit.Test)

Example 9 with AccessControlList

use of com.qcloud.cos.model.AccessControlList in project cos-java-sdk-v5 by tencentyun.

the class AclTest method setObjectCannedAclTest.

@Test
public void setObjectCannedAclTest() throws IOException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(0L);
    String key = "ut/acl_test.txt";
    putObjectFromLocalFile(localFile, key);
    try {
        cosclient.setObjectAcl(bucket, key, CannedAccessControlList.PublicRead);
        AccessControlList accessControlList = cosclient.getObjectAcl(bucket, key);
        assertEquals(accessControlList.getCannedAccessControl(), CannedAccessControlList.PublicRead);
        cosclient.setObjectAcl(bucket, key, CannedAccessControlList.Private);
        accessControlList = cosclient.getObjectAcl(bucket, key);
        assertEquals(accessControlList.getCannedAccessControl(), CannedAccessControlList.Private);
        cosclient.setObjectAcl(bucket, key, CannedAccessControlList.Default);
        accessControlList = cosclient.getObjectAcl(bucket, key);
        assertEquals(accessControlList.getCannedAccessControl(), CannedAccessControlList.Default);
    } finally {
        assertTrue(localFile.delete());
        clearObject(key);
    }
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) File(java.io.File) Test(org.junit.Test)

Example 10 with AccessControlList

use of com.qcloud.cos.model.AccessControlList in project cos-java-sdk-v5 by tencentyun.

the class SetGetObjectAclDemo method setGetObjectAclTest.

public static void setGetObjectAclTest() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    cosclient.putObject(bucketName, key, "data");
    // 设置对象的acl
    AccessControlList acl = new AccessControlList();
    Owner owner = new Owner();
    owner.setId("qcs::cam::uin/2779643970:uin/2779643970");
    acl.setOwner(owner);
    // 设置子账号734505014具有WriteAcp权限
    String id = "qcs::cam::uin/2779643970:uin/734505014";
    UinGrantee uinGrantee = new UinGrantee(id);
    uinGrantee.setIdentifier(id);
    // 设置子账号909619400具有Read权限
    acl.grantPermission(uinGrantee, Permission.WriteAcp);
    String id1 = "qcs::cam::uin/2779643970:uin/909619400";
    UinGrantee uinGrantee1 = new UinGrantee(id1);
    uinGrantee.setIdentifier(id1);
    acl.grantPermission(uinGrantee1, Permission.Read);
    cosclient.setObjectAcl(bucketName, key, acl);
    // 获取对象的acl
    AccessControlList aclGet = cosclient.getObjectAcl(bucketName, key);
    List<Grant> grants = aclGet.getGrantsAsList();
    // private object acl
    cosclient.setObjectAcl(bucketName, key, CannedAccessControlList.Private);
    AccessControlList accessControlList = cosclient.getObjectAcl(bucketName, key);
    System.out.println("private object acl:" + accessControlList.getCannedAccessControl());
    // public-read object acl
    cosclient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
    accessControlList = cosclient.getObjectAcl(bucketName, key);
    System.out.println("public-read object acl:" + accessControlList.getCannedAccessControl());
    // default object acl
    cosclient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
    accessControlList = cosclient.getObjectAcl(bucketName, key);
    System.out.println("default object acl:" + accessControlList.getCannedAccessControl());
}
Also used : COSClient(com.qcloud.cos.COSClient) CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) Owner(com.qcloud.cos.model.Owner) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

AccessControlList (com.qcloud.cos.model.AccessControlList)10 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)9 UinGrantee (com.qcloud.cos.model.UinGrantee)7 Test (org.junit.Test)6 Grant (com.qcloud.cos.model.Grant)5 Owner (com.qcloud.cos.model.Owner)4 COSCredentials (com.qcloud.cos.auth.COSCredentials)3 Region (com.qcloud.cos.region.Region)3 File (java.io.File)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 CreateBucketRequest (com.qcloud.cos.model.CreateBucketRequest)2 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 Bucket (com.qcloud.cos.model.Bucket)1 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)1 Grantee (com.qcloud.cos.model.Grantee)1 GroupGrantee (com.qcloud.cos.model.GroupGrantee)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1