Pre signed URL to OSS does not match

27 Views Asked by At

My react native (0.70.x) acquires pre signed URL from backend nodejs/express (20.x) server before uploading image file to Aliyun OSS. The backend nodejs/express server calculates a pre signed URL based on file name (file name to be used by OSS, as object key), content type (here image/jpeg) and method (PUT) and send it back to react native frontend. Then the react native frontend uses the pre signed URL received to upload the image file to OSS. The problem is that when uploading with fetch PUT, the OSS constantly throws error of signature not match.

Here is the code on nodejs server for pre signed URL calculation:

sUrl = store.signatureUrl(items[i][0]), {. //<<items[I][0] is object key, ex, 22790/fEIX6E_1000000022.jpg
                    expires: parseInt(process.env.OSSExpireIn), //<<== 3600s
                    method: 'PUT',
                    'Content-Type': _type // _type is image/jpeg
                };

Here is the store definition:

store: function() {
    try {
        let bucket_name = 'oss-1', region_name='oss-cn-xxxx'; 
        let store = OSS({
            accessKeyId: process.env.AliStsAccessKeyId,
            accessKeySecret: process.env.AliStsAccessKeySecret,
            bucket: bucket_name,
            region: region_name
          });
        return store;    
    } catch(err) {
        console.log("Error creating store with OSS", err);
        return false;
    };
}, 

Here is the uploading code in react native app:

fileData = await ReactNativeBlobUtil.fs.readFile(filePath, 'base64');  //read local jpeg file 
  //console.log("fileData in uploadtooss : ", Boolean(fileData));
  const formData = new FormData();

  //formData.append('key', filename);
  formData.append('file', fileData, {type:contentTypeMap[extension]}); 
  //contentTypeMap[extension]) is image/jpeg
  const response = await fetch(preSignedUrl, { 
    method: 'PUT',
    body: formData,
  });

Here is the 403 reply in console output:

 LOG  respnose in uploadtooss :  {"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "f0071d9c-f218-4b26-bcaf-0d7e8983bbf3", "offset": 0, "size": 1127}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "f0071d9c-f218-4b26-bcaf-0d7e8983bbf3", "offset": 0, "size": 1127}}, "bodyUsed": false, "headers": {"map": {"connection": "keep-alive", "content-length": "1127", "content-type": "application/xml", "date": "Thu, 08 Feb 2024 22:18:27 GMT", "server": "AliyunOSS", "x-oss-ec": "0002-00000040", "x-oss-request-id": "65C5533307D4B93635D7511C", "x-oss-server-time": "95"}}, "ok": false, "status": 403, "statusText": "", "type": "default", "url": "https://oss-hz-1.oss-cn-xxxxxx.aliyuncs.com/22790-fCWOVB_1000000021.jpg?OSSAccessKeyId=LTAI5t5axvqwo2qGnxxxxxxxx&Expires=1707432506&Signature=m60o9WszMIcfzF8mHWQ2HDPHsHW%3D"}

When using the pre signed URL and jpeg image file in postman, it throws the error:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Code>SignatureDoesNotMatch</Code>
  <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
  <RequestId>646DCB189AE2D1333018****</RequestId>
  <HostId>bucket.oss-cn-xxxxx.aliyuncs.com</HostId>
  <OSSAccessKeyId>LTAI5tMw7e8syBazfpSS****</OSSAccessKeyId>
  <SignatureProvided>tPN3MChhvKk2XixolQLonoJW****</SignatureProvided>
  <StringToSign>PUT\n\n\nTue, 23 May 2023 15:24:55 GMT\n/bucket/?acl</StringToSign>
  <StringToSignBytes>50 55 54 0A 0A 0A 54 75 65 2C 20 32 33 20 4D 61 79 20 32 30 32 33 20 31 35 3A 32 34 3A 35 35 20 47 4D 54 0A 2F 64 69 6E 61 72 79 2F 3F 61 63 6C </StringToSignBytes>
  <EC>0002-00000040</EC>
</Error>

Also tried many combinations, such as without content-type on server and with content-type in front end when uploading. The error is pretty the same 403.

0

There are 0 best solutions below

Related Questions in ALI-CLOUD-STORAGE