예제 #1
0
    def upload_file(self, filename, blob_name=None, connection=None):
        """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()
          >>> bucket = storage.get_bucket('my-bucket', connection=connection)
          >>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
          >>> print bucket.list_blobs()
          [<Blob: my-bucket, remote-text-file.txt>]

        If you don't provide a blob name, we will try to upload the file
        using the local filename (**not** the complete path)::

          >>> from gcloud import storage
          >>> connection = storage.get_connection()
          >>> bucket = storage.get_bucket('my-bucket', connection=connection)
          >>> bucket.upload_file('~/my-file.txt')
          >>> print bucket.list_blobs()
          [<Blob: my-bucket, my-file.txt>]

        :type filename: string
        :param filename: Local path to the file you want to upload.

        :type blob_name: string
        :param blob_name: The name of the blob to upload the file to. 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.

        :type connection: :class:`gcloud.storage.connection.Connection` or
                          ``NoneType``
        :param connection: Optional. The connection to use when sending
                           requests. If not provided, falls back to default.

        :rtype: :class:`Blob`
        :returns: The updated Blob object.
        """
        if blob_name is None:
            blob_name = os.path.basename(filename)
        blob = Blob(bucket=self, name=blob_name)
        blob.upload_from_filename(filename, connection=connection)
        return blob
예제 #2
0
    def upload_file(self, filename, blob_name=None, connection=None):
        """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()
          >>> bucket = storage.get_bucket('my-bucket', connection=connection)
          >>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
          >>> print bucket.list_blobs()
          [<Blob: my-bucket, remote-text-file.txt>]

        If you don't provide a blob name, we will try to upload the file
        using the local filename (**not** the complete path)::

          >>> from gcloud import storage
          >>> connection = storage.get_connection()
          >>> bucket = storage.get_bucket('my-bucket', connection=connection)
          >>> bucket.upload_file('~/my-file.txt')
          >>> print bucket.list_blobs()
          [<Blob: my-bucket, my-file.txt>]

        :type filename: string
        :param filename: Local path to the file you want to upload.

        :type blob_name: string
        :param blob_name: The name of the blob to upload the file to. 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.

        :type connection: :class:`gcloud.storage.connection.Connection` or
                          ``NoneType``
        :param connection: Optional. The connection to use when sending
                           requests. If not provided, falls back to default.

        :rtype: :class:`Blob`
        :returns: The updated Blob object.
        """
        if blob_name is None:
            blob_name = os.path.basename(filename)
        blob = Blob(bucket=self, name=blob_name)
        blob.upload_from_filename(filename, connection=connection)
        return blob
예제 #3
0
    def copydir(self, path):
        """
        Copy the contents of the local directory given by path to google cloud.
        Maintain the same directory structure on remote.
        This is (intentionally) a blocking call, so clients can report errors if
        the transfer fails.

        :type path: string
        :param path: relative or absolute path to the directory that needs to be copied
        :return: True when transfer is complete
        :raises OSError: path doesn't exist or permission denied
        :raises ValueError: if the library cannot determine the file size
        :raises gcloud.exceptions.GCloudError: if upload status gives error response
        """
        if not os.access(path, os.R_OK):
            raise OSError('Permission denied')
        for filename in find_files(path):
            blob = Blob(filename, self)
            blob.upload_from_filename(filename)
        return True