如何修复Android上的'SignatureDoesNotMatch'错误

时间:2019-07-07 21:49:32

标签: android amazon-s3 ionic3 cordova-plugin-advanced-http

我正在尝试使用S3签名的URL从Ionic应用程序上传图像,最令人困惑的部分是相同的代码在iOS上有效,但在Android上无效。

我的应用程序使用的是cordova-plugin-advanced-http,cordova-plugin-camera和cordova-plugin-file。

my-component.ts

openPhotoLibrary() {
    this.loadingProvider.showLoader('Aguarde');
    this.viewCtrl.dismiss();
    this.camera.getPicture(this.photoLibraryOptions).then((fileURI) => {
      this.file.resolveLocalFilesystemUrl(fileURI).then(
        (fileEntry: FileEntry) => {
          console.log('resolveLocalFilesystemUrl');
          console.log(fileEntry);

          fileEntry.file((file) => {
            console.log('file');
            console.log(file);
            let newName = uuid().concat('.jpg');
            console.log('newName');
            console.log(newName);

            this.myProvider.getImageUploadUrl(newName, this.JPEG_CONTENT_TYPE).then(
              (res) => {
                let responseData = JSON.parse(res.data);

                console.log('getImageUploadUrl');
                console.log(responseData);
                console.log('FILE THAT WILL BE SENT -> ', file);

                this.myProvider.uploadImage(responseData.data, file, this.JPEG_CONTENT_TYPE).then(
                  (data) => {
                    console.log('uploadImage');
                    console.log(data);
                    this.loadingProvider.hideLoader();
                  }, (error) => {
                    console.error('uploadImage error: ', error);
                    this.loadingProvider.hideLoader();
                  }
                );
              }, (error) => {
                console.error('getImageUploadUrl error: ', error);
              });
            }, (error) => {
              console.error('fileEntry.file error: ', error);
            }
          );
        }, (error) => {
          console.error('resolveLocalFilesystemUrl error: ', error);
        }
      );
     }, (error) => {
      console.error(error);
     });
  }

my-provider.ts

getImageUploadUrl(fileName: string, fileType: string) {
    const phoneNumber = this.storage.getPhoneNumber();
    const userToken = this.storage.getToken();
    return this.http.get(
      `${API_ROOT}chatimages/getImageUploadUrl?fileName=${fileName}&fileType=${fileType}`, {}, {
        'Authorization': userToken,
        'phoneNumber': phoneNumber
      }
    );
  }

  uploadImage(url: string, image: IFile, imageType: string) {
    console.log('URL');
    console.log(url);
    let host = url.split('?')[0] + '?' + decodeURIComponent(url.split('?')[1]);
    console.log('HOST');
    console.log(host);
    let headers = { 'Accept': '*/*', 'Content-Type': imageType };
    return this.http.put(host, image, headers);
  }

我希望相同的代码在Android上也可以使用,但是我正在尝试解决Android上的此响应错误,当我从提供者处调用uploadImage时,就会发生此错误:

<?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><AWSAccessKeyId>{MY_ACCESSKEYID}</AWSAccessKeyId><StringToSign>PUT

application/x-www-form-urlencoded; charset=UTF-8
1562518033
x-amz-acl:private
/gd-chat-images/1d515a36-163a-4bd0-aaf5-7788066f0e49.jpg</StringToSign><SignatureProvided>1VqI E8JXOHx4RFp/JOLOgw9/Fw=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 3b 20 63 68 61 72 73 65 74 3d 55 54 46 2d 38 0a 31 35 36 32 35 31 38 30 33 33 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 72 69 76 61 74 65 0a 2f 67 64 2d 63 68 61 74 2d 69 6d 61 67 65 73 2f 31 64 35 31 35 61 33 36 2d 31 36 33 61 2d 34 62 64 30 2d 61 61 66 35 2d 37 37 38 38 30 36 36 66 30 65 34 39 2e 6a 70 67</StringToSignBytes><RequestId>8F636E127D004199</RequestId><HostId>akhR/eD2/30T7Xw2nkdWLEwn4kgWX7hOui2NMoJ5hNyh0dmKpxbDAlXIotnZbcpFrrdtmvFM/BM=</HostId></Error>

0 个答案:

没有答案