Logo Search packages:      
Sourcecode: s3ql version File versions  Download package

def s3ql::backends::boto::s3::key::Key::send_file (   self,
  fp,
  headers = None,
  cb = None,
  num_cb = 10 
)
Upload a file to a key into a bucket on S3.

:type fp: file
:param fp: The file pointer to upload

:type headers: dict
:param headers: The headers to pass along with the PUT request

:type cb: function
:param cb: a callback function that will be called to report
    progress on the upload.  The callback should accept two integer
    parameters, the first representing the number of bytes that have
    been successfully transmitted to S3 and the second representing
    the total number of bytes that need to be transmitted.
    
:type num_cb: int
:param num_cb: (optional) If a callback is specified with the cb
       parameter this parameter determines the granularity
       of the callback by defining the maximum number of
       times the callback will be called during the file
       transfer. Providing a negative integer will cause
       your callback to be called with each buffer read.
     

Definition at line 361 of file key.py.

                                                             :
        """
        Upload a file to a key into a bucket on S3.
        
        :type fp: file
        :param fp: The file pointer to upload
        
        :type headers: dict
        :param headers: The headers to pass along with the PUT request
        
        :type cb: function
        :param cb: a callback function that will be called to report
                    progress on the upload.  The callback should accept two integer
                    parameters, the first representing the number of bytes that have
                    been successfully transmitted to S3 and the second representing
                    the total number of bytes that need to be transmitted.
                    
        :type num_cb: int
        :param num_cb: (optional) If a callback is specified with the cb
                       parameter this parameter determines the granularity
                       of the callback by defining the maximum number of
                       times the callback will be called during the file
                       transfer. Providing a negative integer will cause
                       your callback to be called with each buffer read.
             
        """
        def sender(http_conn, method, path, data, headers):
            http_conn.putrequest(method, path)
            for key in headers:
                http_conn.putheader(key, headers[key])
            http_conn.endheaders()
            fp.seek(0)
            save_debug = self.bucket.connection.debug
            self.bucket.connection.debug = 0
            if cb:
                if num_cb > 2:
                    cb_count = self.size / self.BufferSize / (num_cb - 2)
                elif num_cb < 0:
                    cb_count = -1
                else:
                    cb_count = 0
                i = total_bytes = 0
                cb(total_bytes, self.size)
            l = fp.read(self.BufferSize)
            while len(l) > 0:
                http_conn.send(l)
                if cb:
                    total_bytes += len(l)
                    i += 1
                    if i == cb_count or cb_count == -1:
                        cb(total_bytes, self.size)
                        i = 0
                l = fp.read(self.BufferSize)
            if cb:
                cb(total_bytes, self.size)
            response = http_conn.getresponse()
            body = response.read()
            fp.seek(0)
            self.bucket.connection.debug = save_debug
            if response.status == 500 or response.status == 503 or \
                    response.getheader('location'):
                # we'll try again
                return response
            elif response.status >= 200 and response.status <= 299:
                self.etag = response.getheader('etag')
                if self.etag != '"%s"' % self.md5:
                    raise S3DataError('ETag from S3 did not match computed MD5')
                return response
            else:
                raise S3ResponseError(response.status, response.reason, body)

        if not headers:
            headers = {}
        else:
            headers = headers.copy()
        headers['User-Agent'] = UserAgent
        headers['Content-MD5'] = self.base64md5
        if self.storage_class != 'STANDARD':
            headers['x-amz-storage-class'] = self.storage_class
        if headers.has_key('Content-Type'):
            self.content_type = headers['Content-Type']
        elif self.path:
            self.content_type = mimetypes.guess_type(self.path)[0]
            if self.content_type == None:
                self.content_type = self.DefaultContentType
            headers['Content-Type'] = self.content_type
        else:
            headers['Content-Type'] = self.content_type
        headers['Content-Length'] = str(self.size)
        headers['Expect'] = '100-Continue'
        headers = boto.utils.merge_meta(headers, self.metadata)
        resp = self.bucket.connection.make_request('PUT', self.bucket.name,
                                                   self.name, headers,
                                                   sender=sender)
        self.handle_version_headers(resp)


Generated by  Doxygen 1.6.0   Back to index