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 {
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject";
String method = "PUT";
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(bucket
, cosPath) {
@Override
public RequestBodySerializer getRequestBody()
throws CosXmlClientException {
return RequestBodySerializer.string("text/plain",
"this is test");
}
};
presignedUrlRequest.setRequestMethod(method);
presignedUrlRequest.setSignKeyTime(60);
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 {
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject";
String method = "GET";
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest(bucket
, cosPath);
presignedUrlRequest.setRequestMethod(method);
presignedUrlRequest.setSignKeyTime(60);
presignedUrlRequest.addNoSignHeader("Host");
String urlWithSign = cosXmlService.getPresignedURL(presignedUrlRequest);
} catch (CosXmlClientException e) {
e.printStackTrace();
}
Note:
For the complete sample, go to GitHub.