Generating a Pre-Signed URL

Last updated: 2021-08-10 15:12:11

    Overview

    This document provides an overview of SDK code samples related to generating pre-signed object links.

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, please see SDK API Reference.

    try {
       String bucket = "examplebucket-1250000000"; // Bucket name
       String cosPath = "exampleobject"; // Location identifier of the object in the bucket.
       String method = "PUT"; // HTTP request method
       PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(bucket
               , cosPath) {
           @Override
           public RequestBodySerializer getRequestBody()
                   throws CosXmlClientException {
               // Used to calculate a pre-signed URL for requests like `PUT` that require a request body
               return RequestBodySerializer.string("text/plain",
                       "this is test");
           }
       };
       presignedUrlRequest.setRequestMethod(method);
        String urlWithSign = cosXmlService.getPresignedURL(presignedUrlRequest);
    } catch (CosXmlClientException e) {
       e.printStackTrace();
    }
    
    Note:

    For more samples, please visit GitHub.

    try {
       String bucket = "examplebucket-1250000000"; // Bucket name
       String cosPath = "exampleobject"; // Location identifier of the object in the bucket.
       String method = "GET"; // HTTP request method
       PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(bucket
               , cosPath);
       presignedUrlRequest.setRequestMethod(method);
        String urlWithSign = cosXmlService.getPresignedURL(presignedUrlRequest);
    } catch (CosXmlClientException e) {
       e.printStackTrace();
    }
    
    Note:

    For more samples, please visit GitHub.