Amazon S3报NoSuchKey错误但对象实际存在的问题排查求助
Hey there, let’s tackle that frustrating NoSuchKey error you’re facing — it’s so annoying when you know the object exists but the generated link won’t work. Let’s walk through some common fixes and checks to get this sorted:
1. Double-check your object key (the most likely culprit!)
Looking at your code, you’re using the key public/public/us-east-2:/1597842961073/family.jpg. This extra public/ prefix and the us-east-2:/ segment might be throwing things off.
Head over to your AWS S3 console, navigate directly to the family.jpg object, and copy the exact object key from the "Object overview" tab. S3 keys don’t include region identifiers like us-east-2:/ unless you explicitly added them when uploading.
For example, if your object lives at s3://your-bucket/public/1597842961073/family.jpg, the correct key to pass to Storage.get() is just public/1597842961073/family.jpg. Try updating your code with this corrected key first:
fetchAvatar = async () => { try { // Use the exact key copied from S3 console const imageData = await Storage.get("public/1597842961073/family.jpg") console.log(imageData) } catch (err) { console.log('error fetching avatar: ') console.log(err) } }
2. Verify Amplify Storage region matches your S3 bucket
If your S3 bucket is in us-east-2, make sure your Amplify configuration is set to use the same region. Mismatched regions can cause Storage.get to generate URLs pointing to the wrong bucket location, leading to NoSuchKey errors.
Check your amplifyconfiguration.json file (look for the Storage section) or your Amplify initialization code to confirm the region matches your bucket’s region.
3. Test a direct S3 URL to isolate the issue
To rule out problems with Amplify’s URL generation, construct a direct S3 URL for your object and test it in a browser:https://<your-bucket-name>.s3.us-east-2.amazonaws.com/<exact-object-key>
If this direct URL works, the issue is definitely with how you’re passing the key to Storage.get(). If it still throws NoSuchKey, double-check that you copied the key correctly (no hidden spaces, typos, or extra slashes).
4. Rule out permission-related "hidden" issues
S3 sometimes returns NoSuchKey instead of AccessDenied as a security measure, even if the real problem is permissions. To confirm permissions are working:
- Use the AWS CLI to run:
aws s3 cp s3://<your-bucket-name>/<exact-object-key> ./test.jpg
If this command succeeds, your permissions are fine, and you can focus back on key format or Amplify config. If it fails, revisit your bucket policy or IAM permissions to ensure public access is properly configured.
5. Check for accidental duplicate prefixes
If you used Amplify’s Storage upload API, it automatically adds the public/ prefix for public objects. If you manually added another public/ when uploading, you’d end up with a key like public/public/... — which would require passing that full key to Storage.get(). But this is less likely unless you customized the upload logic.
内容的提问来源于stack exchange,提问作者Stevan Najeeb

