Android Volley 放置文件 AWS S3 签名请求

Android Volley put file AWS S3 Signed request

我正在尝试使用签名请求将文件上传到 android 中的 AWS S3。

我尝试执行 this code,但出现错误 403:

<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>xxx</AWSAccessKeyId>
    <StringToSign>PUT
      x-www-form-urlencoded; charset=UTF-8 <!-- Not wanted parameters -->
      expireTimeStamp
      /my-url
    </StringToSign>
    <SignatureProvided>xxx</SignatureProvided>
    <StringToSignBytes>xxx</StringToSignBytes>
    <RequestId>xxx</RequestId>
    <HostId>xxx</HostId>
</Error>

那么,如何从截击请求中删除x-www-form-urlencoded; charset=UTF-8

简单:

@Override
public String getBodyContentType()
{
    return "";
}

确保 headers:

Content-Type

和 在 body

二进制数据:

public static byte[] getBytes(Context context, Uri uri) {
    InputStream iStream = null;
    try {
        iStream = context.getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    byte[] inputData = new byte[0];
    try {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len;
        while ((len = iStream != null ? iStream.read(buffer) : 0) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        inputData = byteBuffer.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return inputData;

}


@Override
public String getBodyContentType() {
     return null; 
}