tencent cloud

masukan

Generating Pre-Signed URLs

Terakhir diperbarui:2024-06-25 10:53:13

    Overview

    This document provides an overview of SDK code samples related to generating pre-signed object URLs. For details about how to use a pre-signed URL for uploads, see Upload via Pre-Signed URL. For details about how to use a pre-signed URL for downloads, see Download via Pre-Signed URL.
    Note:
    You are advised to use a temporary key to generate pre-signed URLs for the security of your requests such as uploads and downloads. When you apply for a temporary key, follow the Principle of Least Privilege to avoid leaking resources besides your buckets and objects.
    If you need to use a permanent key to generate a pre-signed URL, you are advised to limit the permission of the permanent key to uploads and downloads only to avoid risks.

    SDK API References

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

    Generating a Pre-Signed Object URL

    Sample code 1. Generating a pre-signed upload URL

    try {
    // Bucket name
    String bucket = "examplebucket-1250000000";
    // Object key, the unique location ID of an object in a bucket. For more information, see [Object Key](https://www.tencentcloud.com/document/product/436/13324).
    // Note: The key does not need to be encoded.
    String cosPath = "exampleobject";
    // HTTP request method
    String method = "PUT";
    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);
    // Set the signature validity period to be 60s. Note that here is the signature validity period. You need to ensure the key validity period by yourself.
    presignedUrlRequest.setSignKeyTime(60);
    // Set not to sign `Host`
    presignedUrlRequest.addNoSignHeader("Host");
    String urlWithSign = cosXmlService.getPresignedURL(presignedUrlRequest);
    } catch (CosXmlClientException e) {
    e.printStackTrace();
    }
    Note:
    For the complete sample, go to GitHub.

    Sample code 2. Generating a pre-signed download URL

    try {
    // Bucket name
    String bucket = "examplebucket-1250000000";
    // Object key, the unique location ID of an object in a bucket. For more information, see [Object Key](https://www.tencentcloud.com/document/product/436/13324).
    // Note: The key does not need to be encoded.
    String cosPath = "exampleobject";
    // HTTP request method
    String method = "GET";
    PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(bucket
    , cosPath);
    presignedUrlRequest.setRequestMethod(method);
    
    // Set the signature validity period to be 60s. Note that here is the signature validity period. You need to ensure the key validity period by yourself.
    presignedUrlRequest.setSignKeyTime(60);
    // Set not to sign `Host`
    presignedUrlRequest.addNoSignHeader("Host");
    
    String urlWithSign = cosXmlService.getPresignedURL(presignedUrlRequest);
    
    } catch (CosXmlClientException e) {
    e.printStackTrace();
    }
    Note:
    For the complete sample, go to GitHub.
    Hubungi Kami

    Hubungi tim penjualan atau penasihat bisnis kami untuk membantu bisnis Anda.

    Dukungan Teknis

    Buka tiket jika Anda mencari bantuan lebih lanjut. Tiket kami tersedia 7x24.

    Dukungan Telepon 7x24