Welcome, guest | Sign In | My Account | Store | Cart

A small script that allows you to push files and directories to S3 using a context menu in nautilus file browser.

Add this script to ~/.gnome2/nautilus-scripts/ and be sure it is executable. Requires boto, the python aws library. Credentials by default are looked up from ~/.boto but can be supplied in the get_s3_conn() function.

Python, 48 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

import mimetypes
import os
import sys

import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key


def get_s3_conn():
    return S3Connection()

def get_bucket(conn, name):
    return conn.get_bucket(name)

og = os.environ.get
bucket_name = og('NAUTILUS_BUCKET_NAME', 'media.foo.com')
bucket_prefix = og('NAUTILUS_BUCKET_PREFIX', 'scrapspace/files')

conn = get_s3_conn()
bucket = get_bucket(conn, bucket_name)

def get_ctype(f):
    return mimetypes.guess_type(f)[0] or "application/x-octet-stream"

def put_file(filename, keyname):
    new_key = Key(bucket)
    new_key.key = keyname
    new_key.set_metadata('Content-Type', get_ctype(filename))
    new_key.set_contents_from_filename(filename)

if __name__ == '__main__':
    for name in sys.argv[1:]:
        full = os.path.abspath(name)
        if os.path.isdir(name):
            parent_dir = os.path.dirname(full)
            for base, directories, files in os.walk(full):
                for filename in files:
                    full_path = os.path.join(base, filename)
                    rel_path = os.path.relpath(full_path, parent_dir)
                    keyname = os.path.join(bucket_prefix, rel_path)
                    put_file(full_path, keyname)
        else:
            filename = os.path.basename(name)
            keyname = os.path.join(bucket_prefix, filename)
            put_file(filename, keyname)