How can I access s3 files in Python using urls?

后端 未结 6 1174
一个人的身影
一个人的身影 2020-12-14 16:44

I want to write a Python script that will read and write files from s3 using their url\'s, eg:\'s3:/mybucket/file\'. It would need to run locally and in the cloud without a

6条回答
  •  萌比男神i
    2020-12-14 17:05

    For opening, it should be as simple as:

    import urllib
    opener = urllib.URLopener()
    myurl = "https://s3.amazonaws.com/skyl/fake.xyz"
    myfile = opener.open(myurl)
    

    This will work with s3 if the file is public.

    To write a file using boto, it goes a little something like this:

    from boto.s3.connection import S3Connection
    conn = S3Connection(AWS_KEY, AWS_SECRET)
    bucket = conn.get_bucket(BUCKET)
    destination = bucket.new_key()
    destination.name = filename
    destination.set_contents_from_file(myfile)
    destination.make_public()
    

    lemme know if this works for you :)

提交回复
热议问题