amplify-js: Storage.get('file') should throw an error if file does not exist

Do you want to request a feature or report a bug? Feature

What is the current behavior?

Storage.get(‘file’) resolves to a presigned URL, whether or not the file exists.

This is a problem because the URL results in a 404, which means an extra step is needed to check the validity of the URL.

It’s especially a problem in Angular, when using constructs such as the following in the ionic aws starter project.

account.ts, lines 42-45

  refreshAvatar() {
    Storage.get(this.userId + '/avatar')
      .then(url => this.avatarPhoto = (url as string));
  }

account.html, line 12 <div *ngIf="avatarPhoto" class="avatar" [style.background-image]="'url('+ avatarPhoto +')'">

What is the expected behavior?

Storage.get(‘file’) should return an error that can be handled using catch or if (error).

The current documentation doesn’t specify what is returned on failure, so this change won’t be a problem there.

However, this would be a breaking change for code that relies on the current behaviour. One option is to simply accept the breaking change. Another option is to add a third parameter to the method, for the sake of discussion, called “safe,” to request this new behaviour.

Which versions of Amplify, and which browser / OS are affected by this issue? Did this work in previous versions?

0.4.6

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 23
  • Comments: 26 (6 by maintainers)

Most upvoted comments

I’m currently working with someone who experiencing this issue as well. The problem is that they can’t call download because the files are very large and that would be a terrible user experience, and doing Storage.list() seems like a bad workaround as well. As file number increases it could require looking through 100s or 1000s of items to see if it exists. Is there really no better way to tell if the resource actually exists using Storage.get?

If anyone is using React Native, there is a work-around by checking the image’s size.

Image.getSize(imgUrl, (width, height) => {
    console.log('image exists.')
}, err => {
    console.log('image not found')
})

I missed that validateObjectExistence was introduced in aws-amplify 5.1.0. It works after upgrading to 5.1.0. Thanks and sorry for the confusion.

On Mon, Aug 21, 2023 at 9:12 PM Venkata Ramyasri Kota < @.***> wrote:

Could you please upgrade it to @.*** Ref: release notes https://github.com/aws-amplify/amplify-js/releases/tag/aws-amplify%405.1.0. Please let us know if that does not work.

— Reply to this email directly, view it on GitHub https://github.com/aws-amplify/amplify-js/issues/1145#issuecomment-1686571929, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQFYLIRZ6MT5VC2UTFPKJALXWN6VPANCNFSM4FHSVDRQ . You are receiving this because you commented.Message ID: @.***>

We have added an option validateObjectExistence to check for the existence of the file. Please ref doc for the usage.

Any update for this issue? Currently I’m just using fetch() method from React Native for accessing the presigned URL and make sure that response is 404 if file doesn’t exist.