Object Storage Service


Object Storage Service



You can use Android SDK to manage Object Storage Service; Android Source Code can be downloaded from https://github.com/aws/aws-sdk-android.


Because android sdk uses the chunk method for transmission, and puts chunk-signature on the body, oss does not support such case; the parameter (S3ClientOptions.builder.disableChunkedEncoding) supported in sdk has not been taken upon judgment, thus, hack in deeper level is required, it is required to inherit AWSS3V4Signer.java and replace original processRequestPayload (calculate the signature of payload and put it at the beginning of body) and calculateContentHash (the length includes the signature part), the code as follows:

package com.amazonaws.demo.s3transferutility;
 import com.amazonaws.AmazonClientException;
 import com.amazonaws.Request;
 import com.amazonaws.services.s3.Headers;
 import com.amazonaws.services.s3.internal.AWSS3V4Signer;
 import com.amazonaws.util.BinaryUtils;
 import java.io.IOException;
 import java.io.InputStream;
 public class JdAWSS3V4Signer extends AWSS3V4Signer {
     protected void processRequestPayload(Request<?> request, HeaderSigningResult headerSigningResult) {
     protected String calculateContentHash(Request<?> request) {
         request.addHeader("x-amz-content-sha256", "required");
         final String contentLength =
         final long originalContentLength;
         if (contentLength != null) {
             originalContentLength = Long.parseLong(contentLength);
         } else {
              * "Content-Length" header could be missing if the caller is
              * uploading a stream without setting Content-Length in
              * ObjectMetadata. Before using sigv4, we rely on HttpClient to
              * add this header by using BufferedHttpEntity when creating the
              * HttpRequest object. But now, we need this information
              * immediately for the signing process, so we have to cache the
              * stream here.
             try {
                 originalContentLength = getContentLength(request);
             } catch (IOException e) {
                 throw new AmazonClientException(
                         "Cannot get the content-lenght of the request content.",
         final InputStream payloadStream = getBinaryRequestPayloadStream(request);
         final String contentSha256 = BinaryUtils.toHex(hash(payloadStream));
         try {
         } catch (final IOException e) {
             throw new AmazonClientException(
                     "Unable to reset stream after calculating AWS4 signature", e);
         return contentSha256;
     private long getContentLength(Request<?> request) throws IOException {
         InputStream content = request.getContent();
         if (!content.markSupported()) {
             throw new AmazonClientException("Failed to get content length");
         final int DEFAULT_BYTE_LENGTH = 4096;
         long contentLength = 0;
         byte[ ] tmp = new byte[DEFAULT_BYTE_LENGTH];
         int read;
         while ((read = content.read(tmp)) != -1) {
             contentLength += read;
         return contentLength;

Then use SignerFactory.registerSigner("JdAWSS3V4Signer", JdAWSS3V4Signer.class) to replace the standard signature, the implementation code is as follows:

SignerFactory.registerSigner("JdAWSS3V4Signer", JdAWSS3V4Signer.class);
 System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
 ClientConfiguration config = new ClientConfiguration();
 sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()), config);
 S3ClientOptions options = S3ClientOptions.builder()

Demo Download

Download link

Update Time:2019-05-28 16:47:18