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 any code changes. Is there a way to do this?
Edit: There are some good suggestions here but what I really want is something that allows me to do this:
myfile = open("s3://mybucket/file", "r")
and then use that file object like any other file object. That would be really cool. I might just write something like this for myself if it doesn't exist. I could build that abstraction layer on simples3 or boto.
I haven't seen something that would work directly with S3 urls, but you could use an S3 access library (simples3 looks decent) and some simple string manipulation:
http://s3tools.org/s3cmd works pretty well and support the s3:// form of the URL structure you want. It does the business on Linux and Windows. If you need a native API to call from within a python program then http://code.google.com/p/boto/ is a better choice.
You can use Boto Python API for accessing S3 by python. Its a good library. After you do the installation of Boto, following sample programe will work for you
You can find more information here http://boto.cloudhackers.com/s3_tut.html#storing-data
For opening, it should be as simple as:
This will work with s3 if the file is public.
To write a file using boto, it goes a little something like this:
lemme know if this works for you :)
Here's how they do it in awscli :
Which you could just use with code like this
This doesn't address the goal of interacting with an s3 key as a file like object but it's a step in that direction.
Try s3fs
First example on the docs: