Search code examples
amazon-s3jcloudshttp-content-length

JClouds S3: Specify Content Length when uploading file


I wrote an application using JClouds 1.6.2 and had file upload code like

java.io.File file = ...
blobStore.putBlob(containerName,
    blobStore.blobBuilder(name)
    .payload(file)
    .calculateMD5()
    .build()
);

This worked perfectly well.

Now, in jclouds 1.7, BlobStore.calculateMD5() is deprecated. Furthermore, even if calculating the MD5 hash manually (using guava Hashing) and passing it with BlobStore.contentMD5(), I get the following error:

java.lang.IllegalArgumentException: contentLength must be set, streaming not supported

So obviously, I also have to set the content length.

What is the easiest way to calculate the correct content length?

Actually I don't think, jclouds suddenly removed support of features and makes uploading files much more difficult. Is there a way to let jclouds calculate MD5 and/or content length?


Solution

  • You should work with ByteSource which offers several helper methods:

    ByteSource byteSource = Files.asByteSource(new File(...));
    Blob blob = blobStore.blobBuilder(name)
            .payload(byteSource)
            .contentLength(byteSource.size())
            .contentMD5(byteSource.hash(Hashing.md5()).asBytes())
            .build();
    blobStore.putBlob(containerName, blob);
    

    jclouds made these changes to remove functionality duplicated by Guava and make some of the costs of some operations, e.g., hashing, more obvious.