Python GCP-上传文件到GCP桶



我从gmail收件箱中获得csv文件,我使用gmail api,一旦我获得这些csv文件,我不想将它们存储在我的本地,我想从GCP连接导入这些文件到我的桶,但我无法从csv文件中获得我的路径。

upload_to_bucket(file_name, attachment_content,bucket_name)
File "/Users/Emails/mypython”, line 91, in upload_to_bucket
blob.upload_from_filename(file_path)
File "/Users/.local/share/virtualenvs/Emails-wLEQ9xGC/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 2704, in upload_from_filename
content_type = self._get_content_type(content_type, filename=filename)
File "/Users/.local/share/virtualenvs/Emails-wLEQ9xGC/lib/python3.10/site-packages/google/cloud/storage/blob.py", line 1674, in _get_content_type
content_type, _ = mimetypes.guess_type(filename)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/mimetypes.py", line 307, in guess_type
return _db.guess_type(url, strict)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/mimetypes.py", line 123, in guess_type
scheme, url = urllib.parse._splittype(url)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/parse.py", line 1039, in _splittype
match = _typeprog.match(url)
TypeError: cannot use a string pattern on a bytes-like object

我已经测试了这些csv文件从收件箱到我的本地,但知道我添加了"upload_bycket"我试图从附件中获取它,但找不到

import os
import base64
from typing import List
import time
from datetime import datetime
from Google import Create_Service
from google.cloud import storage
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'mystorageaccont.json'
storage_client = storage.Client()
bucket_name = 'mybucketname'
def upload_to_bucket(blob_name, file_path, bucket_name):
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
blob.upload_from_filename(file_path)
return blob
def search_emails(query_stirng: str, label_ids: List=None):
try:
message_list_response = service.users().messages().list(
userId='me',
labelIds=label_ids,
q=query_string
).execute()
message_items = message_list_response.get('messages')
next_page_token = message_list_response.get('nextPageToken')

while next_page_token:
message_list_response = service.users().messages().list(
userId='me',
labelIds=label_ids,
q=query_string,
pageToken=next_page_token
).execute()
message_items.extend(message_list_response.get('messages'))
next_page_token = message_list_response.get('nextPageToken')
return message_items
except Exception as e:
raise NoEmailFound('No emails returned'
)

def get_file_data(message_id, attachment_id, file_name, new_Location):
response = service.users().messages().attachments().get(
userId='me',
messageId=message_id,
id=attachment_id
).execute()
file_data = base64.urlsafe_b64decode(response.get('data').encode('UTF-8'))
return file_data

def get_message_detail(message_id, msg_format='metadata', metadata_headers: List=None):
message_detail = service.users().messages().get(
userId='me',
id=message_id,
format=msg_format,
metadataHeaders=metadata_headers
).execute()
return message_detail
def save_file_data(email_messages):
for email_message in email_messages: 
messageDetail = get_message_detail(email_message['id'], msg_format='full', metadata_headers=['parts'])  
headers=messageDetail["payload"]["headers"]
messageDetailPayload = messageDetail.get('payload') 
if 'parts' in messageDetailPayload: 
for msgPayload in messageDetailPayload['parts']: 
file_name = msgPayload['filename'] 
filetype = ".csv"
if file_name.find(filetype) != -1:
body = msgPayload['body'] 
if 'attachmentId' in body: 
attachment_id = body['attachmentId'] 
attachment_content = get_file_data(email_message['id'], attachment_id, file_name, save_location) 
upload_to_bucket(file_name, attachment_content,bucket_name)
if __name__ == '__main__': 
CLIENT_FILE = 'mycredentialsforconnectgmail.json' 
API_NAME = 'gmail' 
API_VERSION = 'v1' 
SCOPES = ['https://mail.google.com/'] 
service = Create_Service(CLIENT_FILE, API_NAME, API_VERSION, SCOPES) 
query_string = 'has:attachment' 
email_messages = search_emails(query_string) 
save_file_data(email_messages)


我也做了一个小的应用程序使用文件从我的本地和它的工作,但现在的路径是我的变量从收件箱

您的错误显示为cannot use a string pattern on a bytes-like object

Python 3以字节为单位给出数据。你不能对它进行编码。修改代码

file_data = base64.urlsafe_b64decode(response.get('data').encode('UTF-8'))

file_data = base64.urlsafe_b64decode(response.get('data'))

相关内容

  • 没有找到相关文章

最新更新