Question

as stated, i am trying to upload a file to s3

require 'digest/md5' 
require 'base64' 
require 'aws-sdk'

def digest f

 f.rewind

 Digest::MD5.new.tab do |dig|

  f.each_chunk{|ch| dig << ch}

 end.base64digest

ensure

 f.rewind

end 

file = File.new(compress file) #file zipped with zip/zip

total = file.size

digest = digest(file)

s3 = AWS::S3::new(:access_key_id => @access_key_id, :secret_access_key
=> @secret_access_key)

bucket = s3.buckets['mybucket']

bucket.objects["myfile"].write :content_md5 => digest, :content_length
=> total do |buf,len|

 buf.write(file.read len)

end

but i constantly get AWS::S3::Errors::BadDigest exception

if i try to upload the file without passing :content_md5, everything goes well, archive downloads and opens correctly.

also as i just found out this fails on ruby 1.9.3 but works well on 1.9.2

Was it helpful?

Solution

fixed by changing digest func to

def digest f

Digest::MD5.file(f.path).base64digest

end

i think the issue was in the fact that the file passed to it was open

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top