ali-sdk / ali-oss

Aliyun OSS(Object Storage Service) JavaScript SDK for the Browser and Node.js
https://www.alibabacloud.com/help/doc-detail/52834.htm
MIT License
1.94k stars 578 forks source link

InvalidDigestError #388

Closed mrzzcn closed 6 years ago

mrzzcn commented 6 years ago

部分用户上传失败:

{
    "line": 1,
    "column": 531494,
    "sourceURL": "xxx.min.js",
    "name": "InvalidDigestError",
    "status": 400,
    "code": "InvalidDigest",
    "requestId": "5AA9F2D694911C9912DD1E13",
    "params":
    {
        "object": "xxx.jpeg",
        "bucket": "xxx",
        "method": "PUT",
        "headers":
        {},
        "mime": "",
        "content":
        {
            "type": "Buffer",
            "data": []
        },
        "successStatuses": [200]
    }

应该怎么解决?

binghaiwang commented 6 years ago

用户发送请求时携带Content-MD5,OSS会计算上传数据的MD5与用户上传的MD5值比较,如果不一致返回InvalidDigest错误码

mrzzcn commented 6 years ago

@binghaiwang

.put(objectKey, buffer)

我没有手动开启上传时添加MD5,是自动的吗,能不能关掉? 错误仅在部分用户使用时发生,不确认客户端计算MD5算法跟服务器是否相同?

binghaiwang commented 6 years ago

最好能够抓包看一下? 怀疑是不是自己带了头上去, sdk不会带这个请求头的,都是用户调用传入

mrzzcn commented 6 years ago

是SDK传的:

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9
authorization:OSS xxx
Cache-Control:no-cache
Connection:keep-alive
Content-Length:132362
Content-Md5:GacEsmbodYEJw7h2PimK+w==
Content-Type:image/png
Host:xxx.com
Origin:https://xxx
Pragma:no-cache
Referer:xxx
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1
x-oss-date:Thu, 15 Mar 2018 09:06:25 GMT
x-oss-security-token:xxx
x-oss-user-agent:aliyun-sdk-js/4.13.2 Chrome Mobile 56.0.2924.75 (like Safari 8+) on Apple iPhone (iOS 10.3)

@binghaiwang

mrzzcn commented 6 years ago

有人能帮忙看下问题吗?

binghaiwang commented 6 years ago

你这边是不是传进来的是Buffer呢, 如果是buffer sdk确实会自动计算md5

PeterRao commented 6 years ago

上传代码贴一下

mrzzcn commented 6 years ago

@binghaiwang @PeterRao 是buffer

return Promise
        .all([
            convertBlobToBuffer(file),
            prepareAliyunOssClient()
        ])
        .then(([buffer, aliOss]) => {
            return aliOss.put(objectKey, buffer);
        })
        .then(response => {
            return {
                code: 200,
                data: {
                    name: response.name,
                    url: response.url
                }
            };
        })

这边因为场景是选取图片之后,压缩上传,所以直接传了Buffer,转成文件上传就可以了?

binghaiwang commented 6 years ago

sdk目前没办法关闭 ,浏览器端直接使用file用multipartUpload这个接口上传, 参数objectKey, file即可

mrzzcn commented 6 years ago

好的,谢谢。

binghaiwang commented 6 years ago

@mrzzcn convertBlobToBuffer(file) 你这个里边是怎么转的呢 , 我这边 file转Buffer 是上传md5校验是可以的

mrzzcn commented 6 years ago

@binghaiwang

export default function (blob) {
    return new Promise((resolve, reject) => {
        if (typeof Blob === 'undefined' || !(blob instanceof Blob)) {
            throw new Error('first argument must be a Blob')
        }
        const reader = new FileReader();

        function onLoadEnd() {
            reader.removeEventListener('loadend', onLoadEnd, false);
            resolve(Buffer.from(reader.result));
        }

        reader.addEventListener('loadend', onLoadEnd, false);
        reader.addEventListener('error', function(error) {
            reader.removeEventListener('error');
            reject(error);
        }, false);
        reader.readAsArrayBuffer(blob);
    });
}
PeterRao commented 6 years ago

最后返回的错误请求的response body 完整的发下

mrzzcn commented 6 years ago
{
    "line": 1,
    "column": 531494,
    "sourceURL": "https://s.xx.cn/app/app-crm-rack/vendor.WiA2TCLW8Y_GCc.min.js",
    "name": "InvalidDigestError",
    "status": 400,
    "code": "InvalidDigest",
    "requestId": "5AA9F2D694911C9912DD1E13",
    "hostId": "xxx.aliyuncs.com",
    "params":
    {
        "object": "h5.536f6e9.jpeg",
        "bucket": "xxx",
        "method": "PUT",
        "headers":
        {},
        "mime": "",
        "content":
        {
            "type": "Buffer",
            "data": []
        },
        "successStatuses": [200]
    }