ABS
ABS

Reputation: 49

How to fetch an image from one bucket and put it in another in S3 boto

I want to fetch an image from one bucket(bucket1) and put it another bucket (bucket2) I have written the below code snippet in python which I am trying,but I am getting the error:IOError: cannot identify image file.Please help.Thanks!

 conn = boto.connect_s3()
 filename='image2.jpg'
 bucket=conn.get_bucket('bucket1')
 key= bucket.get_key(filename)
 fp = open (filename, "w+")
 key.get_file (fp)


img = cStringIO.StringIO(fp.read())
im = Image.open(img)

b = conn.get_bucket('bucket2')
k = b.new_key('image2.jpg')
k.set_contents_from_string(out_im2.getvalue())

Upvotes: 1

Views: 1316

Answers (1)

AChampion
AChampion

Reputation: 30258

Why not just use copy_key to avoid downloading and writing the object:

b = conn.get_bucket('bucket2')
k = b.copy_key('image2.jpg', 'bucket1', 'image2.jpg')

As you want to do some image processing you need to seek back to the beginning of the file after writing the file:

fp.seek(0, 0)
img = cStringIO.StringIO(fp.read())
im = Image.open(img)

b = conn.get_bucket('bucket2')
k = b.new_key('image2.jpg')
k.set_contents_from_string(...)

Upvotes: 2

Related Questions