def upload_file(cls, uri, fobj): """ Given a file-like object, upload it to the specified URI. :param str uri: The URI to upload the file to. :param file fobj: The file-like object to populate the S3 key from. :rtype: :py:class:`boto.s3.key.Key` :returns: The newly set boto key. """ # Breaks the URI into usable componenents. values = get_values_from_media_uri(uri) logger.debug("S3Backend.upload_file(): Received: %s" % values) conn = cls._get_aws_s3_connection(values['username'], values['password']) bucket = conn.create_bucket(values['host']) key = bucket.new_key(values['path']) logger.debug("S3Backend.upload_file(): "\ "Settings contents of '%s' key from %s" % ( values['path'], fobj.name)) key.set_contents_from_filename(fobj.name) logger.debug("S3Backend.upload_file(): Upload complete.") return key
def download_file(cls, uri, fobj): """ Given a URI, download the file to the ``fobj`` file-like object. :param str uri: The URI of a file to download. :param file fobj: A file-like object to download the file to. :rtype: file :returns: A file handle to the downloaded file. """ # Breaks the URI into usable componenents. values = get_values_from_media_uri(uri) conn = cls._get_aws_s3_connection(values['username'], values['password']) bucket = conn.get_bucket(values['host']) key = bucket.get_key(values['path']) logger.debug("S3Backend.download_file(): " \ "Downloading: %s" % uri) try: key.get_contents_to_file(fobj) except AttributeError: # Raised by ResumableDownloadHandler in boto when the given S3 # key can't be found. message = "The specified input file cannot be found." raise InfileNotFoundException(message) logger.debug("S3Backend.download_file(): " \ "Download of %s completed." % uri) return fobj
def test_valid_uri_no_path_parsing(self): """ If a path is omitted, assume that they're talking about the root path. """ valid_uri = 'http://some-hostname.org:80' values = get_values_from_media_uri(valid_uri) self.assertEqual(values['path'], '/')
def get_backend_for_uri(uri): """ Given a URI string , return a reference to the storage backend class capable of interacting with the protocol seen in the URI. :param str uri: The URI to find the appropriate storage backend for. :rtype: ``StorageBackend`` :returns: A reference to the backend class to use with this URI. """ values = get_values_from_media_uri(uri) return get_backend_for_protocol(values['protocol'])
def test_valid_uri_minimal_parsing(self): """ Test a valid URI with a more minimal value. """ valid_uri = 'http://some-hostname.org:80/some_dir/some_file.mpg' values = get_values_from_media_uri(valid_uri) self.assertEqual(values['protocol'], 'http') self.assertEqual(values.has_key('username'), False) self.assertEqual(values.has_key('password'), False) self.assertEqual(values['host'], 'some-hostname.org') self.assertEqual(values['path'], 'some_dir/some_file.mpg') self.assertEqual(values['port'], '80')
def test_valid_uri_nopass_parsing(self): """ Test a valid URI with everything but port and password specified. """ valid_uri = 'http://SOMEUSER@some_host.com/some_dir/some_file.mpg' values = get_values_from_media_uri(valid_uri) self.assertEqual(values['protocol'], 'http') self.assertEqual(values['username'], 'SOMEUSER') self.assertEqual(values.has_key('password'), False) self.assertEqual(values['host'], 'some_host.com') self.assertEqual(values['path'], 'some_dir/some_file.mpg') self.assertEqual(values.has_key('port'), False)
def test_valid_uri_parsing(self): """ Test a valid URI with most options specified (aside from port). """ valid_uri = 's3://SOMEUSER:SOME/PASS@some_bucket/some_dir/some_file.mpg' values = get_values_from_media_uri(valid_uri) self.assertEqual(values['protocol'], 's3') self.assertEqual(values['username'], 'SOMEUSER') self.assertEqual(values['password'], 'SOME/PASS') self.assertEqual(values['host'], 'some_bucket') self.assertEqual(values['path'], 'some_dir/some_file.mpg') self.assertEqual(values.has_key('port'), False)