action:GetObject) or access the specified resources (such as resource:examplebucket-1250000000/exampleobject.txt).
To prevent data security risks caused by unexpected and unauthorized operations due to excessive permissions, we strongly recommend you not authorize a user to access all resources (such as resource:*) or perform all operations (such as action:*).examplebucket-1250000000/data/config.json and examplebucket-1250000000/video/ but include examplebucket-1250000000/* in the permission policy, then all objects in the bucket can be downloaded without your authorization, leading to unexpected data leakage.examplebucket-1250000000/data/config.json and examplebucket-1250000000/video/ but include examplebucket-1250000000/* in the permission policy, then all objects in the bucket can be uploaded without your authorization, which may overwrite unintended objects. To avoid this risk, in addition to following the principle of least privilege, you can retain all versions of data for traceability as instructed in Overview.cos:GetBucket) but configured cos:* in the permission policy, then all operations on the bucket will be allowed, including reauthorizing the bucket, deleting objects, and deleting the bucket, which puts your data at extremely high risk.exampleobject.jpg or a directory, e.g., examplePrefix/*. Unless needed, do not grant any user the access to all of your resources using the * wildcard.policy field as described in GetFederationToken to grant limited permissions on operations and resources. For more information about how to generate a temporary key, see Generating and Using Temporary Keys.exampleObject.txt object in the examplebucket-1250000000 bucket, the configuration code should be as follows:// Import `java sts sdk` using the integration method with Maven as described on GitHubimport java.util.*;import org.json.JSONObject;import com.tencent.cloud.CosStsClient;public class Demo {public static void main(String[] args) {TreeMap<String, Object> config = new TreeMap<String, Object>();try {String secretId = System.getenv("secretId");// User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.String secretKey = System.getenv("secretKey");// User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.// Replace it with your own SecretIdconfig.put("SecretId", secretId);// Replace it with your own SecretKeyconfig.put("SecretKey", secretKey);// Validity period of the temporary key, in seconds. Default value: 1800; maximum value: 7200config.put("durationSeconds", 1800);// Replace it with your own bucketconfig.put("bucket", "examplebucket-1250000000");// Replace it with the region where your bucket residesconfig.put("region", "ap-guangzhou");// Change it to the allowed path prefix (such as "a.jpg", "a/*", or "*"). You can determine the upload path based on your login status.// If "*" is entered, you allow the user to access all resources. Unless otherwise necessary, grant the user only the limited permissions that are needed following the principle of least privilege.config.put("allowPrefix", "exampleObject.txt");// List of key permissions. The following permissions are required for simple upload, upload using a form, and multipart upload. For other permissions, please visit https://www.tencentcloud.com/document/product/436/31923?from_cn_redirect=1String[] allowActions = new String[] {// Download data"name/cos:GetObject"};config.put("allowActions", allowActions);JSONObject credential = CosStsClient.getCredential(config);// If it succeeds, the temporary key information will be returned and printed out as shown below:System.out.println(credential);} catch (Exception e){// If it fails, an exception will be thrownthrow new IllegalArgumentException("no valid secret !");}}}
exampleObject.txt object in the examplebucket-1250000000 bucket and all objects in the examplePrefix directory, the access policy should be as follows:{"version": "2.0","statement": [{"action":["name/cos:GetObject"],"effect": "allow","resource": ["qcs::cos:ap-beijing:uid/1250000000:examplebucket-1250000000/exampleObject.txt","qcs::cos:ap-beijing:uid/1250000000:examplebucket-1250000000/examplePrefix/*"]}]}
content-type and content-language). The Java code sample is as follows:// Pass in the obtained temporary key (tmpSecretId, tmpSecretKey, sessionToken)String tmpSecretId = "SECRETID";String tmpSecretKey = "SECRETKEY";String sessionToken = "TOKEN";COSCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);// Set the bucket region. For abbreviations of COS regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1// `clientConfig` contains the set methods to set region, HTTPS (HTTP by default), timeout, and proxy. For detailed usage, please see the source code or the FAQs about the SDK for Java.Region region = new Region("COS_REGION");ClientConfig clientConfig = new ClientConfig(region);// To generate a URL that uses the HTTPS protocol, configure this line (recommended).// clientConfig.setHttpProtocol(HttpProtocol.https);// Generate a COS client.COSClient cosClient = new COSClient(cred, clientConfig);// Enter the Bucket name in the format of `BucketName-APPID`.String bucketName = "examplebucket-1250000000";// Object key, the unique identifier of the object in the bucket.String key = "exampleobject";GeneratePresignedUrlRequest req =new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);// Set the http header returned for download.ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();String responseContentType = "image/x-icon";String responseContentLanguage = "zh-CN";// Set the returned header to contain filename information.String responseContentDispositon = "filename=\\"exampleobject\\"";String responseCacheControl = "no-cache";String cacheExpireStr =DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24L * 3600L * 1000L));responseHeaders.setContentType(responseContentType);responseHeaders.setContentLanguage(responseContentLanguage);responseHeaders.setContentDisposition(responseContentDispositon);responseHeaders.setCacheControl(responseCacheControl);responseHeaders.setExpires(cacheExpireStr);req.setResponseHeaders(responseHeaders);// Setting the signature expiration time (optional). If it is not configured, the signature expiration time in ClientConfig (1 hour) is used by default.// Set the signature to expire in half an hour.Date expirationDate = new Date(System.currentTimeMillis() + 30L * 60L * 1000L);req.setExpiration(expirationDate);URL url = cosClient.generatePresignedUrl(req);System.out.println(url.toString());cosClient.shutdown();
100000000001 permission to download the exampleObject.txt object in the examplebucket-1250000000 bucket, the access policy should be as follows:{"version": "2.0","principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000001"]},"statement": [{"action":["name/cos:GetObject"],"effect": "allow","resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000.ap-guangzhou.myqcloud.com/exampleObject.txt"]}]}
100000000011 (root account UIN: 100000000001) permission to download the objects in the examplePrefix directory in the examplebucket-1250000000 bucket, the access policy should be as follows:{"version": "2.0","principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000011"]},"statement": [{"action":["name/cos:GetObject"],"effect": "allow","resource": ["qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000.ap-guangzhou.myqcloud.com/examplePrefix/*"]}]}
100000000011 (root account UIN: 100000000001) permission to download the exampleObject.txt object in the examplebucket-1250000000 bucket and all objects in the examplePrefix directory, the access policy should be as follows:{"Statement":[{"Action":["name/cos:GetObject"],"Effect": "allow","Principal":{"qcs":["qcs::cam::uin/100000000001:uin/100000000011"]},"Resource":["qcs::cos:ap-beijing:uid/1250000000:examplebucket-1250000000/exampleObject.txt","qcs::cos:ap-beijing:uid/1250000000:examplebucket-1250000000/examplePrefix/*"]}],"version": "2.0"}
masukan