本文整理汇总了Python中googleapiclient.http.MediaIoBaseUpload方法的典型用法代码示例。如果您正苦于以下问题:Python http.MediaIoBaseUpload方法的具体用法?Python http.MediaIoBaseUpload怎么用?Python http.MediaIoBaseUpload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googleapiclient.http
的用法示例。
在下文中一共展示了http.MediaIoBaseUpload方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: object_put
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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%.")
示例2: upload
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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)
示例3: upload_and_replace_file
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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
示例4: run
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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}
示例5: credentials_storage_put
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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))
示例6: upload_chunked_part
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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)
示例7: ext_upload_chunked_part
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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)
示例8: store_file_from_memory
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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)
示例9: run
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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
示例10: put
# 需要导入模块: from googleapiclient import http [as 别名]
# 或者: from googleapiclient.http import MediaIoBaseUpload [as 别名]
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