This document provides an overview of SDK code samples related to generating pre-signed object URLs.
Note:
- You are advised to use a temporary key to generate a pre-signed URL 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.
For the parameters and method descriptions of all the APIs in the SDK, 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);
// 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.
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);
// 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.
Was this page helpful?