question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storage.get( { download: true } ) does not get latest version of S3 bucket file after modifying

See original GitHub issue

Unsure if this is a bug, but encountered some weird caching behaviour with Storage.get() when retrieving file data contents, so this should be clarified. I have an Amplify project that gets data from a text file stored in an S3 bucket made with initializing Amplify storage. The issue is when recent updates are made on a specific file, like updating the file directly on the management console or calling Storage.put(), Calling Storage.get() with { download: true } to get the file contents will still retrieve the unmodified version of the file, and not the latest version; this issue persists for some short period of time until get() eventually retrieves the latest. I see that the changes are reflected in the metadata of the file when I call Storage.list(), so I am not sure why Storage.get does not get the changed file immediately. I doubt if this is expected behaviour, why does this happen? Any way to resolve this issue?

Project was initialized with amplify init and S3 storage resources were made by calling amplify add storage.

A short example of how I use Storage.get():

Storage.get('file-name', { download: true }) .then(data => { data.Body.text().then(string => { // handle the String data return String }); }) .catch(e => { // handle error });

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
weicommented, Nov 18, 2020

Same as #7095

By default, the HTTP Headers returned by S3 looks something like follows:

HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: *
Last-Modified: Wed, 18 Nov 2020 16:53:16 GMT
Access-Control-Allow-Methods: GET, HEAD, PUT, POST, DELETE
Access-Control-Expose-Headers: x-amz-server-side-encryption, x-amz-request-id, x-amz-id-2, ETag
Date: Wed, 18 Nov 2020 17:49:49 GMT
Content-Length: 14
Accept-Ranges: bytes
ETag: "XXXXXXXX"
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: XXXXXXXXxX
Server: AmazonS3
x-amz-request-id: XXXXXXXXXX

Note there is no Cache-Control or Expires headers. When those headers are missing AND Last-Modified header is present, browsers calculates what’s called Heuristic freshness and sets its own ttl in cache. e.g. Firefox docs

This results in subsequent requests getting cached by default as seen:

image

As Harry mentioned in https://github.com/aws-amplify/amplify-js/issues/7095#issuecomment-728355794 Doing something like

Storage.get(filename, { download: true, cacheControl: 'no-cache' })

will get S3 to return the appropriate Cache-Control headers which eliminates this issue.

1reaction
Jaftemcommented, Sep 1, 2020

Similar issue (that’s also unresolved) here: #6413

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storage - Download files - JavaScript - AWS Amplify Docs
If download is true, Storage.get returns an object with a Body field of type Blob . It is up to you to programmatically...
Read more >
Using versioning in S3 buckets - AWS Documentation
Versioning in Amazon S3 is a means of keeping multiple variants of an object in the same bucket. You can use the S3...
Read more >
downloading from AWS S3 while file is being updated
The new object is written separately, the index is updated, and then the old object is purged (unless bucket versioning is enabled, in...
Read more >
How to download ALL versions of all files within a Bucket
There are some s3 applications/tools which recognize versioning feature and either allow you to download one versioned object at a time...
Read more >
Python, Boto3, and AWS S3: Demystified - Real Python
Click on the Download .csv button to make a copy of the credentials. You will need them to complete your setup. Now that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found