Python googleapiclient.http.MediaIoBaseUpload() Examples
The following are 10
code examples of googleapiclient.http.MediaIoBaseUpload().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
googleapiclient.http
, or try the search function
.
Example #1
Source File: __init__.py From starthinker with Apache License 2.0 | 6 votes |
def object_put(auth, path, data, mimetype='application/octet-stream'): bucket, filename = path.split(':', 1) service = get_service('storage', 'v1', auth) media = MediaIoBaseUpload(data, mimetype=mimetype, chunksize=CHUNKSIZE, resumable=True) request = service.objects().insert(bucket=bucket, name=filename, media_body=media) response = None errors = 0 while response is None: error = None try: status, response = request.next_chunk() if project.verbose and status: print("Uploaded %d%%." % int(status.progress() * 100)) except HttpError as e: if e.resp.status < 500: raise error = e except (httplib2.HttpLib2Error, IOError) as e: error = e errors = (errors + 1) if error else 0 if errors > RETRIES: raise error if project.verbose: print("Uploaded 100%.")
Example #2
Source File: storage.py From forseti-security with Apache License 2.0 | 6 votes |
def upload(self, bucket, object_name, file_content): """Upload an object to a bucket. Args: bucket (str): The id of the bucket to insert into. object_name (str): The name of the object to write. file_content (file): An open file object of the content to write to the object. Returns: dict: The resource metadata for the object. """ body = { 'name': object_name } verb_arguments = { 'bucket': bucket, 'body': body, 'media_body': http.MediaIoBaseUpload(file_content, 'application/octet-stream'), } return self.execute_command(verb='insert', verb_arguments=verb_arguments)
Example #3
Source File: action.py From insightconnect-plugins with MIT License | 5 votes |
def run(self, params={}): filename = params.get('file').get('filename') file_bytes = params.get('file').get('content') file_type = params.get('google_file_type') folder_id = params.get('folder_id') # Apply mime_type. Set mime_type to unknown by default. will allow for additions to this action later mime_type = 'application/vnd.google-apps.unknown' if file_type == 'Docs': mime_type = 'application/vnd.google-apps.document' if file_type == 'Sheets': mime_type = 'application/vnd.google-apps.spreadsheet' if file_type == 'Slides': mime_type = 'application/vnd.google-apps.presentation' file_bytes = BytesIO(b64decode(file_bytes)) media = MediaIoBaseUpload(file_bytes, 'file/Komand', resumable=True) if folder_id: file_metadata = {'name': filename, 'mimeType': mime_type, 'parents': [folder_id]} else: file_metadata = {'name': filename, 'mimeType': mime_type} newfile = self.connection.service.files().create(body=file_metadata, media_body=media, supportsTeamDrives=True, fields='id',).execute().get('id') url = 'https://docs.google.com' if file_type == 'Docs': url = url + '/document/d/' + newfile if file_type == 'Sheets': url = url + '/spreadsheets/d/' + newfile if file_type == 'Slides': url = url + '/presentation/d/' + newfile return {'file_id': newfile, 'file_link': url}
Example #4
Source File: storage.py From starthinker with Apache License 2.0 | 5 votes |
def credentials_storage_put(cloud_path, credentials): bucket, filename = cloud_path.split(':',1) data = BytesIO(base64.b64encode(json.dumps(credentials).encode())) media = MediaIoBaseUpload(data, mimetype="text/json") _credentials_retry(_credentials_storage_service().objects().insert(bucket=bucket, name=filename, media_body=media))
Example #5
Source File: uds.py From uds with GNU Affero General Public License v3.0 | 5 votes |
def upload_chunked_part(self, chunk, api=None): """Upload a chunked part to drive and return the size of the chunk :param chunk: :param api: :return: """ if not api: api = self.api with open(chunk.path) as fd: mm = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ) chunk_bytes = mm[chunk.range_start:chunk.range_end] encoded_chunk = encoder.encode(chunk_bytes) file_metadata = { 'name': chunk.media.name + str(chunk.part), 'mimeType': 'application/vnd.google-apps.document', 'parents': [chunk.parent], 'properties': { 'part': str(chunk.part) } } mediaio_file = MediaIoBaseUpload(io.StringIO(encoded_chunk), mimetype='text/plain') self.api.upload_single_file(mediaio_file, file_metadata) return len(chunk_bytes)
Example #6
Source File: uds.py From uds with GNU Affero General Public License v3.0 | 5 votes |
def ext_upload_chunked_part(chunk): """Upload a chunked part to drive and return the size of the chunk""" _api = GoogleAPI() # print("Chunk %s, bytes %s to %s" % (chunk.part, chunk.range_start, chunk.range_end)) with open(chunk.path) as fd: mm = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ) chunk_bytes = mm[chunk.range_start:chunk.range_end] encoded_chunk = encoder.encode(chunk_bytes) file_metadata = { 'name': chunk.media.name + str(chunk.part), 'mimeType': 'application/vnd.google-apps.document', 'parents': [chunk.parent], 'properties': { 'part': str(chunk.part) } } mediaio_file = MediaIoBaseUpload(io.StringIO(encoded_chunk), mimetype='text/plain') _api.upload_single_file(mediaio_file, file_metadata) return len(chunk_bytes)
Example #7
Source File: google.py From pghoard with Apache License 2.0 | 5 votes |
def store_file_from_memory(self, key, memstring, metadata=None, extra_props=None, # pylint: disable=arguments-differ cache_control=None, mimetype=None): upload = MediaIoBaseUpload( BytesIO(memstring), mimetype or "application/octet-stream", chunksize=UPLOAD_CHUNK_SIZE, resumable=True ) return self._upload(upload, key, self.sanitize_metadata(metadata), extra_props, cache_control=cache_control)
Example #8
Source File: fb2cal.py From fb2cal with GNU General Public License v3.0 | 5 votes |
def upload_and_replace_file(service, file_id, metadata, payload): mine_type = 'text/calendar' text_stream = BytesIO(payload) media_body = MediaIoBaseUpload(text_stream, mimetype=mine_type, chunksize=1024*1024, resumable=True) # If file id is provided, update the file, otherwise we'll create a new file if file_id: updated_file = service.files().update(fileId=file_id, body=metadata, media_body=media_body).execute() else: updated_file = service.files().create(body=metadata, media_body=media_body).execute() # Need publically accessible ics file so third party tools can read from it publically permission = { "role": 'reader', "type": 'anyone'} service.permissions().create(fileId=updated_file['id'], body=permission).execute() return updated_file
Example #9
Source File: action.py From insightconnect-plugins with MIT License | 4 votes |
def run(self, params={}): content = params.get('content') file_id = params.get('file_id') new_file_name = params.get('new_file_name') new_mime_type = params.get('new_mime_type') file_bytes = BytesIO(b64decode(content)) # Apply mime_type. mime_type = None if new_mime_type == 'Docs': mime_type = 'application/vnd.google-apps.document' if new_mime_type == 'Sheets': mime_type = 'application/vnd.google-apps.spreadsheet' if new_mime_type == 'Slides': mime_type = 'application/vnd.google-apps.presentation' try: # File's new content. media = MediaIoBaseUpload(file_bytes, mime_type, resumable=True) # Send the request to the API. if new_file_name: updated_file = self.connection.service.files().update( body={'name': new_file_name, 'mimeType': mime_type}, fileId=file_id, media_mime_type=mime_type, media_body=media).execute() else: updated_file = self.connection.service.files().update( body={'mimeType': mime_type}, fileId=file_id, media_mime_type=mime_type, media_body=media).execute() file_id = updated_file['id'] return {'file_id': file_id} except errors.HttpError as error: self.logger.error('An error occurred: %s' % error) raise except: self.logger.error('An unexpected error occurred') raise
Example #10
Source File: bucket_storage.py From kubeface with Apache License 2.0 | 4 votes |
def put( name, input_handle, readers=[], owners=[], mime_type='application/octet-stream'): input_handle.seek(0) (bucket_name, file_name) = split_bucket_and_name(name) # This is the request body as specified: # http://g.co/cloud/storage/docs/json_api/v1/objects/insert#request body = { 'name': file_name, } # If specified, create the access control objects and add them to the # request body if readers or owners: body['acl'] = [] for r in readers: body['acl'].append({ 'entity': 'user-%s' % r, 'role': 'READER', 'email': r }) for o in owners: body['acl'].append({ 'entity': 'user-%s' % o, 'role': 'OWNER', 'email': o }) # Now insert them into the specified bucket as a media insertion. req = get_service().objects().insert( bucket=bucket_name, body=body, # You can also just set media_body=filename, but # for the sake of # demonstration, pass in the more generic file handle, which could # very well be a StringIO or similar. media_body=http.MediaIoBaseUpload(input_handle, mime_type)) resp = req.execute() return resp