Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions drive_cse_upload/_cse_drive_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
"""Drive CSE API."""

import io
from drive_cse_upload._cse_logger import LOGGER
import google
import google.oauth2.service_account
import google_auth_httplib2
from googleapiclient import discovery
from googleapiclient import http
import httplib2


class CseDriveClient(object):
"""Drive CSE API Client."""

ENCRYPTED_MIME_TYPE_PREFIX = 'application/vnd.google-gsuite.encrypted'
OCTET_STREAM = 'application/octet-stream'
CHUNK_SIZE = 8 * 1024 * 1024
NUM_RETRIES = 3

SCOPES = ['https://www.googleapis.com/auth/drive']

Expand All @@ -42,7 +44,7 @@ def __init__(self, sa_key_file):
sa_key_file, scopes=self.SCOPES
)
)
self._http = httplib2.Http()
self._http = http.build_http()
self._delegated_user_email = None

def set_delegated_user(self, delegated_user_email):
Expand Down Expand Up @@ -89,6 +91,7 @@ def cse_upload(
encryption_details,
parent_id,
content_type,
size_hint=0,
):
"""Upload the given data as a new CSE file.

Expand All @@ -100,6 +103,7 @@ def cse_upload(
parent_id: The parent of the file being uploaded. If None, use the root
MyDrive of `delegated_user_email`.
content_type: The content-type of the file being uploaded.
size_hint: the size of the uploaded file, if available.

Returns:
The metadata of the newly uploaded file, as a dict.
Expand All @@ -113,12 +117,25 @@ def cse_upload(
}
if parent_id:
file_metadata['parents'] = [parent_id]
media = http.MediaIoBaseUpload(fd=data_fp, mimetype=self.OCTET_STREAM)
resumable = size_hint > 2 * self.CHUNK_SIZE
media = http.MediaIoBaseUpload(
fd=data_fp,
mimetype=self.OCTET_STREAM,
resumable=resumable,
chunksize=self.CHUNK_SIZE,
)
with self._service() as service:
request = service.files().create(
body=file_metadata, media_body=media, supportsAllDrives=True
)
response = request.execute()
response = None
if resumable:
while response is None:
status, response = request.next_chunk(num_retries=self.NUM_RETRIES)
if status:
LOGGER.debug('Uploaded %s%% ...', int(status.progress() * 100))
else:
response = request.execute()
return response

def cse_update(
Expand All @@ -128,6 +145,7 @@ def cse_update(
filename,
encryption_details,
content_type,
size_hint=0,
):
"""Upload the given data as a new revision of an existing CSE file.

Expand All @@ -137,6 +155,7 @@ def cse_update(
filename: The name of the file being updated.
encryption_details: Encryption metadata for the file being updated.
content_type: The content-type of the file being updated.
size_hint: the size of the uploaded file, if available.

Returns:
The metadata of the newly updated file, as a dict.
Expand All @@ -147,15 +166,28 @@ def cse_update(
'mimeType': self._get_cse_mime_type(content_type),
'clientEncryptionDetails': encryption_details,
}
media = http.MediaIoBaseUpload(fd=data_fp, mimetype=self.OCTET_STREAM)
resumable = size_hint > 2 * self.CHUNK_SIZE
media = http.MediaIoBaseUpload(
fd=data_fp,
mimetype=self.OCTET_STREAM,
resumable=resumable,
chunksize=self.CHUNK_SIZE,
)
with self._service() as service:
request = service.files().update(
fileId=file_id,
body=file_metadata,
media_body=media,
supportsAllDrives=True,
)
response = request.execute()
response = None
if resumable:
while response is None:
status, response = request.next_chunk(num_retries=self.NUM_RETRIES)
if status:
LOGGER.debug('Uploaded %s%% ...', int(status.progress() * 100))
else:
response = request.execute()
return response

def cse_download(self, file_id):
Expand Down
12 changes: 10 additions & 2 deletions drive_cse_upload/cse_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def upload(
driver = _cse_drive_client.CseDriveClient(self._sa_key_file)

content_type = mimetypes.guess_type(input_file)[0]
filename = pathlib.Path(input_file).name
path = pathlib.Path(input_file)
filename = path.name
filesize = path.stat().st_size

driver.set_delegated_user(delegated_user_email)

Expand Down Expand Up @@ -136,7 +138,13 @@ def upload(

LOGGER.debug('Uploading encrypted file to Drive ...')
file = driver.cse_upload(
outp, file_id, filename, encryption_details, parent_id, content_type
outp,
file_id,
filename,
encryption_details,
parent_id,
content_type,
size_hint=filesize,
)
outp.close()

Expand Down
Loading