Pre-signed URL

Last updated: 2020-05-27 14:29:11

    Overview

    The JavaScript SDK provides the API for getting an object URL and the presigned URL for a request.

    Signature calculation

    In all COS XML API requests, the authentication credential Authorization is required for all operations on private resources for COS to determine whether a request is valid.

    There are two ways to use the authentication credential:

    1. Put it in the header parameter; field name: authorization.
    2. Put it in the URL parameter; field name: sign.

    The COS.getAuthorization method is used to calculate the authentication credential (Authorization), which is the signing information used to verify the validity of the request.

    Note:

    This method is recommended only for frontend debugging and not for real projects as there is a risk of key disclosure.

    Use case

    Obtain the authentication credential for object download:

    var Authorization = COS.getAuthorization({
        SecretId: 'COS_SECRETID',
        SecretKey: 'COS_SECRETKEY'
        method: 'GET',
        Key: 'exampleobject',
        Expires: 60,
        Query: {},
        Headers: {}
    });

    Parameter description

    Parameter Name Description Type Required
    SecretId User SecretId String No
    SecretKey User's SecretKey String Yes
    Method HTTP operation method such as GET, POST, DELETE, and HEAD String Yes
    Key Object key (object name) is the unique ID of an object in a bucket. If the request operation is to be performed on a file, this parameter is required and should be a filename. If the operation is on a bucket, this parameter should be left empty String No
    Query Query parameter object of the request Object No
    Headers Header parameter object of the request Object No
    Expires Signature expiration time in seconds. Default value: 900 seconds Number No

    Returned value

    The returned value is the calculated authentication credential string authorization.

    Obtaining pre-signed URL used for requests

    Download request samples

    Sample 1. Get an object URL without signature

    var url = cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Key: 'exampleobject',
        Sign: false
    });

    Sample 2. Get a signed object URL

    var url = cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Key: 'exampleobject'
    });

    Sample 3. Get a signed URL through callback

    If the signing process is asynchronous, you need to get the signed URL through callback.

    cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Key: 'exampleobject',
        Sign: false
    },function (err,data) {
        console.log(err || data.Url);
    });

    Sample 4. Specify the validity period of the link

    cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Key: 'exampleobject',
        Sign: true,
        Expires: 3600, // Unit: second
    },function (err,data) {
        console.log(err || data.Url);
    });

    Sample 5. Get the object URL and download the object

    cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Key: 'exampleobject',
        Sign: true
    },function (err,data) {
        if (err) return console.log(err);
        var downloadUrl = data.Url + (data.Url.indexOf('?') > -1 ? '&' : '?') + 'response-content-disposition=attachment'; // Adds the parameter for a forced download
        window.open(downloadUrl); // This opens the url in a new window. If you need to open the url in the current window, you can use the hidden iframe for download, or use the <a> tag download attribute.
    });

    Upload request samples

    Sample 1. Get a pre-signed URL for Put Object upload

    cos.getObjectUrl({
        Bucket='examplebucket-1250000000',
        Region: 'COS_REGION',     /* Bucket region. Required */
        Method: 'PUT',
        Key: 'exampleobject',
        Sign: true
    },function (err,data) {
        if (err) return console.log(err);
    
        console.log(data.Url);
        var xhr = new XMLHttpRequest();
        xhr.open('PUT', data.Url, true);
        xhr.onload = function (e) {
            console.log(xhr.responseText);
        };
        xhr.onerror = function (e) {
            console.log('error in getting the signature');
        };
        xhr.send();
    });

    Parameter description

    Parameter Name Description Type Required
    Bucket Bucket name in the format: BucketName-APPID. String Yes
    Region Bucket region. For the enumerated values, see Regions and Access Domain Names String Yes
    Key Object key (object name) is the unique ID of an object in a bucket. If the request operation is to be performed on a file, this parameter is required and should be a filename. If the operation is on a bucket, this parameter should be left empty String Yes
    Sign Indicates whether to return a signed URL. Default value: true Boolean No
    Method HTTP operation method such as GET, POST, DELETE, and HEAD. Default value: GET String No
    Query Query parameter object used in the signature calculation Object No
    Headers Header parameter object used in the signature calculation Object No
    Expires Signature expiration time in seconds. Default value: 900 seconds Number No

    Returned value

    The returned value is a string. There are two possible scenarios:

    1. If the signature can be calculated synchronously (for example, the SecretId and SecretKey have been passed in during instantiation), the signed URL will be returned by default.
    2. Otherwise; a URL without a signature will be returned.

    Callback function description

    },function (err,data) {
    Parameter Name Description Type
    err Object returned when an error (network error or service error) occurs. If the request is successful, this is null. For more information, see Error Codes Object
    data Object returned when the request is successful. If the request fails, this is null Object
    - Url Calculated URL String

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help