Amplify-android: No option to get the URL of item in S3 bucket using key.

Created on 20 Jan 2020  路  8Comments  路  Source: aws-amplify/amplify-android

I have files(pictures) in my S3 bucket and I am displaying them on screen. I don't want to use the Amplify.Storage.downloadFile(...)
as it takes a lot of time to download and then render. I want to use a picture loading library like glide and to do that I need to get the URL of the picture (probably by using the key).
Currently there is no such option in android.

Feature Request Storage

Most helpful comment

But I think there should be a simpler way to do it in near future.

There will be! Please continue to follow our project for the upcoming changes and thank you for using Amplify :)

All 8 comments

In JS you can easily get the publically accessible URL by using

Storage.get('test.txt')
    .then(result => console.log(result))
    .catch(err => console.log(err));

Any workaround for doing the same in android?

This is a feature that currently isn't included yet in preview release, but we certainly hope to include it in our upcoming releases.

Temporary workaround for this usecase would be to utilize getEscapeHatch() method to obtain a s3 client with which you can call generatePresignedUrl().

@raphkim Thanks for the response but is it really applicable in my case. I have to access a 'PROTECTED' resource in s3 and there is no configurable option for AmazonS3Client for achieving that. S3ClientOptions does not let me change the access level like StorageDownloadFileOptions does.
How do I call generatePresignedUrl() for a protected image(resource)?

Amplify Storage internally constructs a "service key", which is a combination of StorageDownloadFileOptions, AWSMobileClient#getIdentityId(), and S3 key. This service key can be constructed using S3RequestUtils and be passed directly as a key along with the bucket name to generatePresignedUrl(). I imagine the sample code for temporary workaround would look like the following:

StorageDownloadFileOptions options = StorageDownloadFileOptions.defaultInstance(); // This is your download file options
String identityId = AWSMobileClient.getInstance().getIdentityId();
String serviceKey = S3RequestUtils.getServiceKey(
    options.getAccessLevel(),
    identityId,
    key,
    options.getTargetIdentityId()
);

AmazonS3Client s3 = Amplify.Storage.getEscapeHatch();
URL url = s3.generatePresignedUrl(
    bucketName, 
    serviceKey, 
    null
);

@raphkim Thanks for the workaround. It works perfectly.
But I think there should be a simpler way to do it in near future.

But I think there should be a simpler way to do it in near future.

There will be! Please continue to follow our project for the upcoming changes and thank you for using Amplify :)

In JS you can easily get the publically accessible URL by using

Storage.get('test.txt')
    .then(result => console.log(result))
    .catch(err => console.log(err));

For how long this URL will be valid ?

@ireshdudeja Not sure about JS. In Android, you can set the expiration in seconds:

StorageGetUrlOptions.Builder getUrlOptionsBuilder = StorageGetUrlOptions.builder().expires(7 * 24 * 3600)

Was this page helpful?
0 / 5 - 0 ratings