Upload image available at public URL to S3 using boto

后端 未结 9 1207
广开言路
广开言路 2020-12-23 13:48

I\'m working in a Python web environment and I can simply upload a file from the filesystem to S3 using boto\'s key.set_contents_from_filename(path/to/file). However, I\'d l

9条回答
  •  隐瞒了意图╮
    2020-12-23 14:51

    import boto
    from boto.s3.key import Key
    from boto.s3.connection import OrdinaryCallingFormat
    from urllib import urlopen
    
    
    def upload_images_s3(img_url):
        try:
            connection = boto.connect_s3('access_key', 'secret_key', calling_format=OrdinaryCallingFormat())       
            bucket = connection.get_bucket('boto-demo-1519388451')
            file_obj = Key(bucket)
            file_obj.key = img_url.split('/')[::-1][0]
            fp = urlopen(img_url)
            result = file_obj.set_contents_from_string(fp.read())
        except Exception, e:
            return e
    

提交回复
热议问题