简体   繁体   中英

Ruby aws-sdk - “.exists?” says the file doesn't exist even though I see it in the bucket

I stuck all afternoon on checking whether an uploaded file to AWS S3 exists or not. I use Ruby On Rails and the gem called aws-sdk , v2 .

First of all - the file exists in the bucket, it is located here:

test_bucket/users/10/file_test.pdf

There's no typo, this is the exact path. Also, the bucket + credentials are set up correctly.

And here's how I try to check the existence of the file:

config = {region: 'us-west-1', bucket: AWS_S3_CONFIG['bucket'], key: AWS_S3_CONFIG['access_key_id'], secret: AWS_S3_CONFIG['secret_access_key']}
Aws.config.update({region: config[:region], 
                   credentials: Aws::Credentials.new(config[:key], config[:secret]),
                   :s3 => { :region => 'us-east-1' }})
bucket = Aws::S3::Resource.new.bucket(config[:bucket])

puts bucket.object("file_test.pdf").exists?   

The output is always false .

I also tried puts bucket.object("test_bucket/users/10/file_test.pdf").exists? , but still false .

Also, I tried to make the file public in the AWS S3 dashboard, but no success, still false . The file is visible when click on the generated link.

But the problem is that when I check with using aws-sdk if the file exist, the output is still false .

What am I doing wrong?

Thank you.

您需要将完整路径传递给对象(不包括存储桶名称) users/10/file_test.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM