How to upload a file to S3 without creating a temporary local file

前端 未结 10 1717
無奈伤痛
無奈伤痛 2021-01-30 13:49

Is there any feasible way to upload a file which is generated dynamically to amazon s3 directly without first create a local file and then upload to the s3 server? I use python.

10条回答
  •  太阳男子
    2021-01-30 14:23

    Here is an example downloading an image (using requests library) and uploading it to s3, without writing to a local file:

    import boto
    from boto.s3.key import Key
    import requests
    
    #setup the bucket
    c = boto.connect_s3(your_s3_key, your_s3_key_secret)
    b = c.get_bucket(bucket, validate=False)
    
    #download the file
    url = "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
    r = requests.get(url)
    if r.status_code == 200:
        #upload the file
        k = Key(b)
        k.key = "image1.png"
        k.content_type = r.headers['content-type']
        k.set_contents_from_string(r.content)
    

提交回复
热议问题