Skip to content

Commit

Permalink
Added bucket method to upload files from a file object.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Leyow authored and Kevin Leyow committed Sep 22, 2014
1 parent 1f2aaf3 commit b8c1b24
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from gcloud.storage import exceptions
from gcloud.storage.acl import BucketACL
from gcloud.storage.acl import DefaultObjectACL
Expand Down Expand Up @@ -239,6 +241,51 @@ def upload_file(self, filename, key=None):
key = self.new_key(key)
return key.set_contents_from_filename(filename)

def upload_file_object(self, fh, key=None):
# TODO: What do we do about overwriting data?
"""Shortcut method to upload a file into this bucket.
Use this method to quickly put a local file in Cloud Storage.
For example::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
>>> print bucket.get_all_keys()
[<Key: my-bucket, remote-text-file.txt>]
If you don't provide a key value,
we will try to upload the file using the local filename
as the key
(**not** the complete path)::
>>> from gcloud import storage
>>> connection = storage.get_connection(project, email, key_path)
>>> bucket = connection.get_bucket('my-bucket')
>>> bucket.upload_file(open('~/my-file.txt'))
>>> print bucket.get_all_keys()
[<Key: my-bucket, my-file.txt>]
:type fh: file
:param fh: A file handle open for reading.
:type key: string or :class:`gcloud.storage.key.Key`
:param key: The key (either an object or a remote path)
of where to put the file.
If this is blank,
we will try to upload the file
to the root of the bucket
with the same name as on your local file system.
"""
if key:
key = self.new_key(key)
else:
key = self.new_key(os.path.basename(fh.name))
return key.set_contents_from_file(fh)

def has_metadata(self, field=None):
"""Check if metadata is available locally.
Expand Down

0 comments on commit b8c1b24

Please sign in to comment.