def main(): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('drive', 'v3', credentials=creds) # Call the Drive v3 API results = service.files().list( pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(u'{0} ({1})'.format(item['name'], item['id'])) file_metadata = {'name': 'photo.jpg'} media = MediaFileUpload('photo.jpg', mimetype='image/jpeg') file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() print('File ID: %s' % file.get('id'))
def uploadImage(self, file, file_ext): """ This function uploads the image with given file extension to google drive and create an individual url which can be used to retrieve uniquely. :param file: Input Image path which needs to be uplaoded :type file: str :param file_ext: Input Image extension to upload :type file_ext: str :return: return_url The unique image url which can use to access the image from google drive. :rtype: str """ file_metadata = { 'name': file_ext, # 'mimeType' : 'application/vnd.google-apps.spreadsheet' } folder_list = service.files().list( q="mimeType = 'application/vnd.google-apps.folder'", fields="files(id,name)").execute() for folder in folder_list.get('files'): print('folder title: %s' % str(folder)) if folder['name'] == self.main_upload_dir: folder_id = folder['id'] break if folder_id: file_metadata['parents'] = [folder_id] media = MediaFileUpload(file, mimetype=mime.guess_type( os.path.basename(file))[0], resumable=True) try: file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() except HttpError: print('corrupted file') pass return_url = 'https://drive.google.com/file/d/' + file.get( 'id') + '/view' return return_url
def get_drive_information(api_handler,fileName): try: if not api_handler: return None file_metadata = { 'name': fileName, 'parents': ['1t2wesFIyraxtsA-X7-lWxrnEYnXCnnD6'], 'mimeType': 'image/png' } media = MediaFileUpload(fileName, mimetype='image/png', resumable=True) file = api_handler.files().create(body=file_metadata, media_body=media, fields='id').execute() print ('File ID: ' + file.get('id')) url = "http://drive.google.com/uc?export=view&id={}".format(file.get('id')) print ('url: ' + url) return url except Exception as ex: print(str(ex))
async def upload_file(http, file_path, file_name, mime_type, event): # Create Google Drive service instance drive_service = build("drive", "v2", http=http, cache_discovery=False) # File body description media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True) body = { "title": file_name, "description": "Uploaded using github.com/ravana69/pornhub.", "mimeType": mime_type, } if parent_id: body["parents"] = [{"id": parent_id}] # Permissions body description: anyone who has link can upload # Other permissions can be found at https://developers.google.com/drive/v2/reference/permissions permissions = { "role": "reader", "type": "anyone", "value": None, "withLink": True } # Insert a file file = drive_service.files().insert(body=body, media_body=media_body) response = None while response is None: status, response = file.next_chunk() await asyncio.sleep(5) if status: percentage = int(status.progress() * 100) progress_str = "[{0}{1}]\nProgress: {2}%\n".format( ''.join("█" for i in range(math.floor(percentage / 5))), ''.join("░" for i in range(20 - math.floor(percentage / 5))), round(percentage, 2)) await event.edit( f"Uploading to Google Drive...\n\nFile Name: {file_name}\n{progress_str}" ) if file: await event.edit(file_name + " Uploaded Successfully") # Insert new permissions drive_service.permissions().insert(fileId=response.get('id'), body=permissions).execute() # Define file instance and get url for download file = drive_service.files().get(fileId=response.get('id')).execute() return response.get("webContentLink")
def upload_files(): oauth_config = config['oauth'] credentials = Credentials(None, refresh_token=oauth_config['refresh_token'], token_uri=oauth_config['token_uri'], client_id=get_ecredentials('yatch'), client_secret=get_ecredentials('bakery')) drive = build('drive', 'v3', credentials=credentials) media = MediaFileUpload('{0}.tar.gz'.format(config['timestamp'])) file_metadata = { 'name': config['timestamp'] + '.tar.gz', 'mimeType': 'application/gzip' } resp = drive.files().update(body=file_metadata, fileId=config['backup_file_id'], media_body=media).execute()
def update(service, file_id, filename): try: # retrieve the file from the API. file = service.files().get(fileId=file_id).execute() del file['id'] # else service.files().update() will fail # setup new content. media_body = MediaFileUpload(filename, resumable=True) # update content of the file updated_file = service.files().update(fileId=file_id, body=file, media_body=media_body).execute() except errors.HttpError as error: print('An error occurred: %s' % error) return None print(f"Updated: {filename} ({file_id})") return updated_file
def main(): # if directory empty, do nothing files = os.listdir('./data') if not files: print("Nothing to upload") quit() # creating a drive service connection global drive_service if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) drive_service = build('drive', 'v3', credentials=creds) else: print("Credentials not found.\nPossible error with authentication flow. Exiting...") quit() # upload local files for file in files: path = "./data/" + file if os.path.isfile(path): file_metadata = {'name': file} media = MediaFileUpload(path, resumable=False) drive_file = drive_service.files().create(body=file_metadata, media_body = media, fields='id').execute() print("\n" + file + " Uploaded." + "\nFile ID is " + drive_file.get('id') + "\n") elif os.path.isdir(path): upload_folder(folder_name = file, folder_abs_path = os.path.abspath(path)) elif os.path.islink(path): print("Warning: Symlinks are not followed.") print("Instance: " + os.path.abspath(file)) else: print("FATAL ERROR") # delete local files a = input("Delete ALL local copies? (Y/N)\t") if a == 'y' or a == 'Y': for file in files: if os.path.isdir("./data/" + file): print("Deleting tree: data/" + file) rmtree("./data/" + file) elif os.path.isfile("./data/" + file): print("Deleting data/" + file) os.remove("./data/" + file)
def insert_file(self, title, description, parent_id, mime_type, filename, public=False): """Insert new file. Args: service: Drive API service instance. title: Title of the file to insert, including the extension. description: Description of the file to insert. parent_id: Parent folder's ID. mime_type: MIME type of the file to insert. filename: Filename of the file to insert. Returns: Inserted file metadata if successful, None otherwise. """ media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=False) body = { 'title': title, 'description': description, 'mimeType': mime_type } # Set the parent folder. if parent_id: body['parents'] = [{'id': parent_id}] try: file_obj = self.service.files().insert( body=body, media_body=media_body).execute() if public: # We decided anyone could access with the link of the file self.insert_permission(file_obj['id'], 'anyone', 'anyone', 'reader') return file_obj except errors.HttpError, error: print 'An error occured: %s' % error return None
def upload(fileName, parentID): index = fileName.rindex("/") ts = time.gmtime() simpleFile = fileName[index + 1:] try: cut = simpleFile.index(".py") simpleFile = simpleFile[:cut] except: pass simpleFile += "_" + time.strftime("%Y-%m-%d", ts) + ".py" file_metadata = {'name': simpleFile, "parents": [parentID]} media = MediaFileUpload(fileName, mimetype='file/py') file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() print("File ID:" + file.get('id')) if (file.get('id')): return True return False
def photo_or_video(filename, filepath, drive_service, folder_id): logger.info('Prepare to upload file %s to Google Drive folder %s' % (filepath + filename.encode('utf-8'), folder_id)) file_metadata = {'name': filename, 'parents': [folder_id]} mimetype = get_mimetype(filename) media = MediaFileUpload(filepath + '/' + filename, mimetype=mimetype) logger.info('Uploading file %s to Google Drive folder %s' % (filepath + filename.encode('utf-8'), folder_id)) file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() logger.info('Upload completed for %s File ID: %s' % (filepath + filename.encode('utf-8'), file.get('id')))
def do_ocr(input_file, temp_folder): # Upload the file on Goggle Drive folder_id = new_folder_id mime = 'application/vnd.google-apps.document' file_metadata = { 'name': input_file, 'mimeType': mime, 'parents': [folder_id] } print(file_metadata) media = MediaFileUpload(input_file, mimetype=mime, chunksize=256 * 1024, resumable=True) print(media) Imgfile = service.files().create(body=file_metadata, media_body=media, fields='id') response = None while response is None: status, response = Imgfile.next_chunk() time.sleep(1) if status: print("Uploaded %d%%." % int(status.progress() * 100)) print("Upload of {} is complete.".format(input_file)) basename = "".join(os.path.basename(input_file).split(".")[:-1]) filename = temp_folder + "/" + basename + ".txt" time.sleep(10) # Download the file in txt format from Google Drive getTxt = service.files().export_media(fileId=response['id'], mimeType='text/plain') fh = io.FileIO(filename, 'wb') downloader = MediaIoBaseDownload(fh, getTxt) # downloader.next_chunk() done = False while done is False: status, done = downloader.next_chunk() print("Download %d%%." % int(status.progress() * 100))
async def upload_file(http, file_path, file_name, mime_type, event, parent_id): drive_service = build("drive", "v2", http=http, cache_discovery=False) media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True) body = { "title": file_name, "description": "Uploaded using Ultroid Userbot", "mimeType": mime_type, } if parent_id is not None: body["parents"] = [{"id": parent_id}] permissions = { "role": "reader", "type": "anyone", "value": None, "withLink": True, } file = drive_service.files().insert(body=body, media_body=media_body) response = None display_message = "" while response is None: status, response = file.next_chunk() await asyncio.sleep(1) if status: percentage = int(status.progress() * 100) progress_str = "[{0}{1}]\nProgress: {2}%\n".format( "".join(["●" for i in range(math.floor(percentage / 5))]), "".join(["" for i in range(20 - math.floor(percentage / 5))]), round(percentage, 2), ) current_message = ( f"Uploading to G-Drive:\nFile Name: `{file_name}`\n{progress_str}" ) if display_message != current_message: try: await event.edit(current_message) display_message = current_message except Exception: pass file_id = response.get("id") drive_service.permissions().insert(fileId=file_id, body=permissions).execute() file = drive_service.files().get(fileId=file_id).execute() download_url = file.get("webContentLink") return download_url
def get_rss_etsy(key_words): store = file.Storage('token.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('credentials.json', SCOPES) creds = tools.run_flow(flow, store) service = build('drive', 'v3', http=creds.authorize(Http())) title_list = [] description_list = [] url_list = [] api = EtsyAPI(api_key='trq2ib58r0zqr51mmnlau3yn') url = '&'.join(key_words) url = '/listings/active?keywords=' + url r = api.get(url) data = r.json() for i in range(len(data['results'])): title_list.append(data['results'][i]['title']) description_list.append(data['results'][i]['description']) url_list.append(data['results'][i]['url']) result = zip(title_list, url_list, description_list) feed = feedgenerator.Rss201rev2Feed(title="all events", link="https://etsy.com/", description="New in etsy", language="en") for info in result: feed.add_item(title=info[0], link=info[1], description=info[2], unique_id='no') with open('rss_by_keywords_etsy.rss', 'w') as fp: feed.write(fp, 'utf-8') file_metadata = {'name': 'rss_by_keywords_etsy.rss'} media = MediaFileUpload('rss_by_keywords_etsy.rss', mimetype='text/plain', resumable=True) fili = service.files().create(body=file_metadata, media_body=media, fields='id').execute() o = feed.writeString('utf-8') soup = BeautifulSoup(o, "xml") soup = soup.prettify() with open('templates/rss_by_keywords_etsy.rss', 'w') as fp: fp.write(str(soup))
def google_upload(file_name): # 取得憑證、認證、建立 Google 雲端硬碟 API 服務物件 credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) txtfile = file_name parents = '1vNSHZcNswxLxyUDbwjN4-wwXhqahYeBr' mime = 'application/vnd.google-apps.document' res = service.files().create( body={ 'name': txtfile, 'parents': [parents], 'mimeType': mime }, media_body=MediaFileUpload(txtfile, mimetype=mime, resumable=True)).execute()
def upload(self, bucket, object, filename, mime_type='application/octet-stream'): """ Uploads a local file to Google Cloud Storage. :param bucket: The bucket to upload to. :type bucket: string :param object: The object name to set when uploading the local file. :type object: string :param filename: The local file path to the file to be uploaded. :type filename: string :param mime_type: The MIME type to set when uploading the file. :type mime_type: string """ service = self.get_conn() media = MediaFileUpload(filename, mime_type) response = service \ .objects() \ .insert(bucket=bucket, name=object, media_body=media) \ .execute()
def upload_to_gdrive(files): if files: json_key = json.loads(load_credentials()) credentials = SignedJwtAssertionCredentials( json_key['client_email'], json_key['private_key'].encode(), OAUTH2_SCOPE) http = httplib2.Http() credentials.authorize(http) '''Create Authenticated Drive Service''' drive_service = discovery.build('drive', 'v2', http=http) '''Configure Android or iOS Mode''' mimetype = None upload_folder_id = None if CURRENT_MODE is MODE_ANDROID: mimetype = ANDROID_MIMETYPE upload_folder_id = ANDROID_COMSUMER_FOLDER_ID elif CURRENT_MODE is MODE_IOS: mimetype = IOS_MIMETYPE upload_folder_id = IOS_CONSUMER_FOLDER_ID else: print "\nError! Invalid Mode" return for file_to_upload in files: print("Uploading: " + file_to_upload) filename = os.path.basename(os.path.normpath(file_to_upload)) media_body = MediaFileUpload(file_to_upload, mimetype=mimetype, resumable=True) '''The body contains the metadata for the file''' body = {'title': filename, 'parents': [{'id': upload_folder_id}]} '''Perform the request and print the result''' request = drive_service.files().insert(body=body, media_body=media_body) response = None while response is None: status, response = request.next_chunk() if status: sys.stdout.write("%d%%..." % int(status.progress() * 100)) print "\nUpload Complete!"
def insert(self, file_name, title, parent_id=[], mime_type='application/octet-stream', resumable=True): media_body = MediaFileUpload(file_name, mimetype=mime_type, resumable=resumable) body = {'title': title, 'mimeType': mime_type} if parent_id: body['parents'] = [{'id': parent_id}] try: file = self.drive_service.files().insert( body=body, media_body=media_body).execute() return file except errors.HttpError: return None
def upload_file(service, title, description, parent_id, mime_type, filename): media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True) body = {'title': title, 'description': description, 'mimeType': mime_type} # Set the parent folder. if parent_id: body['parents'] = [{'id': parent_id}] try: file = service.files().insert(body=body, media_body=media_body).execute() #We return the file object. return file except errors.HttpError, error: print 'An error occured: %s' % error #Try again. print("Retrying...") upload_file(service, title, description, parent_id, mime_type, filename) exit(1)
def upload_file(self, filename, filepath, folder=None): file_metadata = {'name': filename} if folder: file_metadata['parents'] = [folder] media = MediaFileUpload(filepath, mimetype='application/octet-stream', resumable=True) request = self.drive_service.files().create(body=file_metadata, media_body=media) response = None while response is None: status, response = request.next_chunk() if status: print("Uploaded %d%%." % int(status.progress() * 100)) print('File ID: %s' % response.get('id')) return response.get('id')
def initialize_upload(youtube, options): tags = None if options.keywords: tags = options.keywords.split(",") body = dict(snippet=dict(title=options.title, description=options.description, tags=tags, categoryId=options.category), status=dict(privacyStatus=options.privacyStatus)) insert_request = youtube.videos().insert(part=",".join(list(body.keys())), body=body, media_body=MediaFileUpload( options.file, chunksize=-1, resumable=True)) resumable_upload(insert_request)
async def upload_to_drive(http, file_path, file_name, mime_type, event, parent_id): drive_service = build("drive", "v2", http=http, cache_discovery=False) media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True) body = { "title": file_name, "description": "Used for hosting OTA Installations - IPAbox", "mimeType": mime_type, } if parent_id is not None: body["parents"] = [{"id": parent_id}] permissions = { "role": "reader", "type": "anyone", "value": None, "withLink": True } file = drive_service.files().insert(body=body, media_body=media_body) response = None display_message = "" while response is None: status, response = file.next_chunk() await asyncio.sleep(1) if status: percentage = int(status.progress() * 100) if display_message != percentage: try: await event.edit(f"Processing `{file_name}`: {percentage}%" ) display_message = percentage except Exception as e: logger.info(str(e)) pass file_id = response.get("id") try: drive_service.permissions().insert(fileId=file_id, body=permissions).execute() except: pass file = drive_service.files().get(fileId=file_id).execute() download_url = file.get("webContentLink") return file_id
def test_resumable_media_fail_unknown_response_code_subsequent_request(self): """Not a multipart upload.""" self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', http=self.http) media_upload = MediaFileUpload(datafile('small.png'), resumable=True) request = zoo.animals().insert(media_body=media_upload, body=None) http = HttpMockSequence([ ({'status': '200', 'location': 'http://upload.example.com'}, ''), ({'status': '400'}, ''), ]) self.assertRaises(HttpError, request.execute, http=http) self.assertTrue(request._in_error_state) http = HttpMockSequence([ ({'status': '308', 'range': '0-5'}, ''), ({'status': '308', 'range': '0-6'}, ''), ]) status, body = request.next_chunk(http=http) self.assertEqual(status.resumable_progress, 7, 'Should have first checked length and then tried to PUT more.') self.assertFalse(request._in_error_state) # Put it back in an error state. http = HttpMockSequence([ ({'status': '400'}, ''), ]) self.assertRaises(HttpError, request.execute, http=http) self.assertTrue(request._in_error_state) # Pretend the last request that 400'd actually succeeded. http = HttpMockSequence([ ({'status': '200'}, '{"foo": "bar"}'), ]) status, body = request.next_chunk(http=http) self.assertEqual(body, {'foo': 'bar'})
def folder_upload(service): '''Uploads folder and all it's content (if it doesnt exists) in root folder. Args: items: List of folders in root path on Google Drive. service: Google Drive service instance. Returns: Dictionary, where keys are folder's names and values are id's of these folders. ''' parents_id = {} for root, _, files in os.walk(FULL_PATH, topdown=True): last_dir = root.split('/')[-1] pre_last_dir = root.split('/')[-2] if pre_last_dir not in parents_id.keys(): pre_last_dir = [] else: pre_last_dir = parents_id[pre_last_dir] folder_metadata = {'name': last_dir, 'parents': [pre_last_dir], 'mimeType': 'application/vnd.google-apps.folder'} create_folder = service.files().create(body=folder_metadata, fields='id').execute() folder_id = create_folder.get('id', []) for name in files: file_metadata = {'name': name, 'parents': [folder_id]} media = MediaFileUpload( os.path.join(root, name), mimetype=mimetypes.MimeTypes().guess_type(name)[0]) service.files().create(body=file_metadata, media_body=media, fields='id').execute() parents_id[last_dir] = folder_id return parents_id
def uploadToDrive(fname): """ Envoie un fichier vers Google Drive Ne retourne rien """ if UploadGoogleDrive == 0: return credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) folder_id = None results = service.files().list( pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: #print('{0} ({1})'.format(item['name'], item['id'])) if item['name'] == "PhotoMariage": folder_id = item['id'] if not folder_id: # create dir file_metadata = { 'name': 'PhotoMariage', 'mimeType': 'application/vnd.google-apps.folder' } file = service.files().create(body=file_metadata, fields='id').execute() print('Created Folder ID: %s' % file.get('id')) folder_id = file.get('id') # upload file file_metadata = {'name': os.path.basename(fname), 'parents': [folder_id]} print("begin uploading ", fname) media = MediaFileUpload(fname, mimetype='image/jpeg', resumable=True) #print("end Upload") file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() print("Created File : %s (%s)" % (file.get('name'), file.get('id')))
def main(): credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) chunksize = 50 * 1024 * 1024 files = [f for f in os.listdir('.') if os.path.isfile(f)] for path, subdirs, files in os.walk('.'): for filename in files: filepath = os.path.join(path, filename) extension = os.path.splitext(filename)[1] mimetype = "" if extension == '.srt': mimetype = "text/plain" elif extension == '.avi': mimetype = "video/avi" elif extension == '.mp4': mimetype = "video/mp4" elif extension == '.mkv': mimetype = "video/webm" else: continue print(filename) media = MediaFileUpload(filepath, mimetype=mimetype, chunksize=chunksize, resumable=True) file_metadata = {'name': filename} request = service.files().create(media_body=media, body=file_metadata, fields='id') response = None while response is None: status, response = request.next_chunk() if status: print("Uploaded %d%%." % int(status.progress() * 100)) print("Upload Complete! - ", response.get('id')) try: os.remove(filepath) print("File removed -", filename) except OSError: pass
def get_rss_amazon(key_words): store = file.Storage('token.json') creds = store.get() if not creds or creds.invalid: flow = client.flow_from_clientsecrets('credentials.json', SCOPES) creds = tools.run_flow(flow, store) service = build('drive', 'v3', http=creds.authorize(Http())) title_list = [] list_rating = [] list_review = [] list_url = [] for i in key_words: results = list(amazonscraper.search(i)) print(i) print(results) for result in results: title_list.append(result.title) list_rating.append(result.rating) list_review.append(result.review_nb) list_url.append(result.url) result = zip(title_list, list_rating, list_review, list_url) feed = feedgenerator.Rss201rev2Feed(title="all events", link="https://www.amazon.com/", description="New in amazon", language="en") for info in result: feed.add_item(title=info[0], link=info[3], description=info[1], unique_id='no') with open('rss_by_keywords_amazon.rss', 'w') as fp: feed.write(fp, 'utf-8') file_metadata = {'name': 'rss_by_keywords_amazon.rss'} media = MediaFileUpload('rss_by_keywords_amazon.rss', mimetype='text/plain', resumable=True) fili = service.files().create(body=file_metadata, media_body=media, fields='id').execute() with open('rss_by_keywords_amazon.rss', 'w') as fp: feed.write(fp, 'utf-8')
def create_file(filename, local_path, mimetype, parent, service, overwrite=False): if parent: existing = get_file_data('match_name_parents', [filename, parent], ['id'], 1, service) if existing: if overwrite: service.files().delete(fileId=existing[0]).execute() else: return existing[0] parent = [parent] else: existing = get_file_data('match_name_parents', [filename, 'root'], ['id'], 1, service) if existing: if overwrite: service.files().delete(fileId=existing[0]).execute() else: return existing[0] parent = [] if mimetype != 'folder' and os.path.getsize(local_path) > 20 * 1000: mimetype = None # Don't convert to Google Doc if file size is large (it's slow) file_metadata = { 'name': filename, 'mimeType': ('application/vnd.google-apps.' + mimetype) if mimetype else None, 'parents': parent } kwargs = {'body': file_metadata, 'fields': 'id'} upload_mimetype = {'document': 'text/plain', 'spreadsheet': 'text/csv'} if mimetype != 'folder' and os.path.getsize(local_path) > 0: kwargs['media_body'] = MediaFileUpload( local_path, mimetype=upload_mimetype[mimetype] if mimetype in upload_mimetype else None, resumable=True) file = service.files().create(**kwargs).execute() return file['id']
def upload(self, bucket, object, filename, mime_type='application/octet-stream', gzip=False): """ Uploads a local file to Google Cloud Storage. :param bucket: The bucket to upload to. :type bucket: string :param object: The object name to set when uploading the local file. :type object: string :param filename: The local file path to the file to be uploaded. :type filename: string :param mime_type: The MIME type to set when uploading the file. :type mime_type: str :param gzip: Option to compress file for upload :type gzip: bool """ service = self.get_conn() if gzip: filename_gz = filename + '.gz' with open(filename, 'rb') as f_in: with gz.open(filename_gz, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) filename = filename_gz media = MediaFileUpload(filename, mime_type) try: service \ .objects() \ .insert(bucket=bucket, name=object, media_body=media) \ .execute() # Clean up gzip file if gzip: os.remove(filename) return True except errors.HttpError as ex: if ex.resp['status'] == '404': return False raise
def upload(): credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http) if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: print('no file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also submit a empty part without filename if file.filename == '': print('no selected file') return redirect(request.url) if file: filename = file.filename print(filename) #set write access to upload folder os.chmod(UPLOAD_FOLDER, 0o777) os.access('files', os.W_OK) # Check for write access os.access('files', os.R_OK) #save file in upload folder file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #get file path filepath=os.path.join(app.config['UPLOAD_FOLDER'], filename) #set file meta data file_metadata = {'name': filename} #set upload file mime type media = MediaFileUpload(filepath, mimetype='image/png') file = service.files().create(body=file_metadata,media_body=media,fields='id').execute() print ('File ID: %s' % file.get('id')) return render_template('success.html')
def RecordVideo(self): if self.video_recording: return self.video_recording = True try: if self.use_light: GPIO.output(PIR_LIGHT, GPIO.HIGH) print("Recording video...") now = datetime.now() # current date and time file_stamp = now.strftime("%m_%d_%Y_%H_%M_%S_sec") file_name = now.strftime("/home/pi/Videos/" + file_stamp) self.command = "raspivid -vf -t " + str( self.record_time) + " -w " + str(self.width) + " -h " + str( self.height) + " -fps " + str( self.fps) + " -b 1200000 -p 0,0," + str( self.width) + "," + str(self.height) command = self.command + " -o " + file_name + ".h264" os.system(command) os.system("MP4Box -add " + file_name + ".h264 " + file_name + ".mp4") os.system("rm " + file_name + ".h264") print("Video finished") if self.use_light: GPIO.output(PIR_LIGHT, GPIO.LOW) except Exception as e: print("Exception in record video: " + str(e)) if self.upload_video: try: print("Uploading video to Google Drive...") file_metadata = {'name': file_stamp + '.mp4'} media = MediaFileUpload(file_name + ".mp4", mimetype='video/mp4') file = self.service.files().create(body=file_metadata, media_body=media, fields='id').execute() print("Video uploaded!") except Exception as e: print("Video upload failed: " + str(e)) self.videos_recorded_from_start += 1 self.SendShadow() self.video_recording = False