简体   繁体   中英

Upload image to S3 Amazon get 0 bytes

I'm trying to upload image png/jpg less than 10 kilobytes to s3 amazon. The upload succeed but the file uploaded and stored with 0 bytes.

When i'm trying to see the image in the link provided by s3- i get blank.

If i upload image more bigger than 10 kilobytes size it's ok.

Can someone have any idea what is the problem please?

file_name = account[:img_token] + File.extname(img.original_filename)
file = Tempfile.new(file_name, encoding: 'ascii-8bit')
file.write(img.read)
path = file.path
bucket_name = 'bucket'
s3 = AWS::S3.new(access_key_id: ENV['S3_ACCESS_KEY'], secret_access_key: ENV['S3_SECRET_ACCESS_KEY'])
link = 'https://s3-eu-west-1.amazonaws.com/' + bucket_name + '/' + file_name
key = file_name
object = s3.buckets[bucket_name].objects[key].write(file: path, acl: 'public-read')

It looks like you are passing a file to the SDK that is seeked to the end of the file. I suspect the SDK is calling #read and getting nil back. Try rewinding the file first.

According to this answer

Write, read, and delete objects containing from 1 byte to 5 terabytes of data each. The number of objects you can store is unlimited.

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