Search in sources :

Example 1 with AnonymousCOSCredentials

use of com.qcloud.cos.auth.AnonymousCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class GeneratePresignedUrlDemo method GeneratePresignedDownloadUrlAnonymous.

// 获取预签名的下载链接, 用于匿名bucket, 匿名bucket生成的预下载链接不包含签名
public static void GeneratePresignedDownloadUrlAnonymous() {
    // 1 初始化用户身份信息, 匿名身份不用传入ak sk
    COSCredentials cred = new AnonymousCOSCredentials();
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa.txt";
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
    URL url = cosclient.generatePresignedUrl(req);
    System.out.println(url.toString());
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig) URL(java.net.URL)

Example 2 with AnonymousCOSCredentials

use of com.qcloud.cos.auth.AnonymousCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class GetObjectURLDemo method getObjectUrlWithEndpoint.

public static void getObjectUrlWithEndpoint() {
    // getObjectUrl 不需要验证身份信息
    COSCredentials cred = new AnonymousCOSCredentials();
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置生成的 url 的协议名
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 设置自定义的域名
    UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("test.endpoint.com", "service.cos.myqcloud.com");
    clientConfig.setEndpointBuilder(endpointBuilder);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test中文.json";
    String bucketName = "mybucket-1251668577";
    String versionId = "xxxyyyzzz111222333";
    System.out.println(cosclient.getObjectUrl(bucketName, key, versionId));
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) UserSpecifiedEndpointBuilder(com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig)

Example 3 with AnonymousCOSCredentials

use of com.qcloud.cos.auth.AnonymousCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testRequestSpecifiedKeyInfoPutGetDel.

@Test
public void testRequestSpecifiedKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
    COSClient cosclient = new COSClient(new AnonymousCOSCredentials(), clientConfig);
    File localFile = buildTestFile(1024L);
    COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
    try {
        String key = "ut/request-specified-key";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        putObjectRequest.setCosCredentials(cosCredentials);
        cosclient.putObject(putObjectRequest);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        getObjectRequest.setCosCredentials(cosCredentials);
        cosclient.getObject(getObjectRequest);
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
        deleteObjectRequest.setCosCredentials(cosCredentials);
        cosclient.deleteObject(deleteObjectRequest);
    } finally {
        localFile.delete();
        cosclient.shutdown();
    }
}
Also used : DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 4 with AnonymousCOSCredentials

use of com.qcloud.cos.auth.AnonymousCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class GetObjectURLDemo method getObjectUrlWithVersionId.

public static void getObjectUrlWithVersionId() {
    // getObjectUrl 不需要验证身份信息
    COSCredentials cred = new AnonymousCOSCredentials();
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置生成的 url 的协议名
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test中文.json";
    String bucketName = "mybucket-1251668577";
    String versionId = "xxxyyyzzz111222333";
    System.out.println(cosclient.getObjectUrl(bucketName, key, versionId));
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig)

Example 5 with AnonymousCOSCredentials

use of com.qcloud.cos.auth.AnonymousCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class GetObjectURLDemo method getObjectUrl.

public static void getObjectUrl() {
    // getObjectUrl 不需要验证身份信息
    COSCredentials cred = new AnonymousCOSCredentials();
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置生成的 url 的协议名
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test中文.json";
    String bucketName = "mybucket-1251668577";
    System.out.println(cosclient.getObjectUrl(bucketName, key));
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)6 COSCredentials (com.qcloud.cos.auth.COSCredentials)6 Region (com.qcloud.cos.region.Region)5 COSClient (com.qcloud.cos.COSClient)4 ClientConfig (com.qcloud.cos.ClientConfig)4 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 File (java.io.File)2 URL (java.net.URL)2 Test (org.junit.Test)2 UserSpecifiedEndpointBuilder (com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder)1 AccessControlList (com.qcloud.cos.model.AccessControlList)1 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)1 DeleteObjectRequest (com.qcloud.cos.model.DeleteObjectRequest)1 GeneratePresignedUrlRequest (com.qcloud.cos.model.GeneratePresignedUrlRequest)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 Date (java.util.Date)1