About the error record about oss using the sts background to issue a temporary token front-end direct transfer of large files

foreword

insert image description here

insert image description here
insert image description here

problems encountered

1. NoSuchBucket : The specified bucket does not exist.

The problem occurs when executing the following code:

// 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
DefaultProfile.addEndpoint("", "", "Sts", ENDPOINT);
// 进行角色授权 构造default profile(参数留空,无需添加region ID)
IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
// 用profile构造client
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setMethod(MethodType.POST);
request.setRoleArn(roleArn);  // role-Arn
request.setRoleSessionName(roleSessionName);
 request.setDurationSeconds(durationSeconds);  // 3600s
// 针对该临时权限可以根据该属性赋予规则,格式为json,没有特殊要求,默认为空
// request.setPolicy(policy); // Optional
final AssumeRoleResponse response = client.getAcsResponse(request);
AssumeRoleResponse.Credentials credentials = response.getCredentials();
final AssumeRoleResponse response = client.getAcsResponse(request);

Then, looking for information, I accidentally found that a blogger specially reminded me at the end:

The endpoint is not the endpoint of oss but the endpoint of sts.
If the endpoint of oss is used, an error will be reported: bucket not exists.

Supplement: Refer to the sts document link: sts access point

2. com.aliyuncs.exceptions.ClientException: InvalidParameter.RoleSessionName : The parameter RoleSessionName is wrongly formed.

The reason is that I set roleSessionName to an empty string.

private static final String roleSessionName = "";

Change to:

private static final String roleSessionName = "alice";

The official example given by this parameter is just an example, and it may not be an empty string.

3. 报错:Access to XMLHttpRequest at ‘上传url’ from origin ‘本地url’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Screenshot of the specific error message:

insert image description here
The first time I saw this, I actually thought that there was a problem with the configuration of cross-domain rules in the Alibaba Cloud console. In the screenshot, the upload method is PUT, so I quickly checked the configuration rules. Sure enough, the previous ones only configured GET, POST. (PS: Since I took over the transformation, I can try not to change the original settings as much as possible, and look for problems from the code first). Finally, after changing the Alibaba Cloud cross-domain configuration rules, this problem was solved.

To make up, cross-domain configuration:
① Enter oss in the console to enter the oss console;
② Open the bucket list and click on any bucket;
③ Click Data Security ——> Cross-domain settings;
④ Add cross-domain rules:
insert image description here
over~

4.报错:Multipart upload ‘xxx’ failed === ConnectionTimeoutError: Connect timeout for 60000ms

insert image description here
The docs have this to say:

When using the MultipartUpload interface, if a ConnectionTimeoutError timeout problem is encountered, the business side needs to handle the timeout logic by itself. For example, handle timeouts by reducing the fragment size, increasing the timeout period, retrying requests, or catching ConnectionTimeoutError errors. For more information, see Network Error Handling .

My solution to this problem is to set the fragmentation to 100MB and set the timeout period:

const options = {
    
    
    // 获取分片上传进度、断点和返回值。
    progress: (p, cpt, res) => {
    
    
        console.log(p);
    },
    // 设置并发上传的分片数量。
    parallel: 4,
    // 设置分片大小。默认值为1 MB,最小值为100 KB。
    partSize: 1024 * 1024 * 100,
    headers,
    // 自定义元数据,通过HeadObject接口可以获取Object的元数据。
    //meta: { year: 2020, people: "test" },
    mime: "text/plain",
    timeout: 120000  // 设置超时时间
};

5. 报错:Error: Please set the etag of expose-headers in OSS

Screenshot:
insert image description here
Then follow the prompts to view this help document: https://help.aliyun.com/document_detail/32069.html
and then click Install
in the prerequisites Note here:
insert image description here
here:
insert image description here

ETag
x-oss-request-id
x-oss-version-id

6. 报错:OperationNotSupportedError: This operation don’t support x-oss-storage-class.

The screenshot is as follows:
insert image description here
I checked the http error code and oss ​​error troubleshooting, but found no introduction to this error. I just found that when setting headers, there is a setting for this item, so I had to comment it out.
Original headers: (written according to the official document example)

const headers = {
    
    
   // 指定该Object被下载时的网页缓存行为。
   "Cache-Control": "no-cache",
   // 指定该Object被下载时的名称。
   //"Content-Disposition": "example.txt",
   // 指定该Object被下载时的内容编码格式。
   "Content-Encoding": "utf-8",
   // 指定过期时间,单位为毫秒。
   //Expires: "1000",
   "Access-Control-Allow-Origin": "*",
   // 指定Object的存储类型。
   //"x-oss-storage-class": "Standard",
   // 指定Object标签,可同时设置多个标签。
   "x-oss-tagging": "Tag1=1&Tag2=2",
   // 指定初始化分片上传时是否覆盖同名Object。此处设置为true,表示禁止覆盖同名Object。
   "x-oss-forbid-overwrite": "true",
   "Content-Type": 'application/x-www-form-urlencoded'
};

I feel that commenting out is not a good solution, although this error will no longer be reported, haha.

Guess you like

Origin blog.csdn.net/qq_36256590/article/details/125995315