Je ne pense pas qu'il y ait beaucoup de besoin, mais j'ai facilité le partage de fichiers tout en enregistrant les fichiers dans S3 sur le stockage d'un autre Vender. Ce n'était pas difficile si j'utilisais GoogleDriveApi pour suivre la documentation, mais il y avait des moments où la taille du fichier était importante. Je pense qu'il y a encore une implémentation similaire, je la laisserai comme note de service au cas où.
Transférez automatiquement les données S3 vers Google Drive.
Migrer le contenu du compartiment S3 avec Python vers le lecteur d'équipe spécifié.
C'est la source que j'ai réellement implémentée.
# Download File from S3 to Local tmp Dir
# Upload a file to Google Drive
import os
import boto3
import json
import requests
import magic
## setting info
CONTENT_BUCKET_NAME = 'MY_S3_BUCKET_NAME'
CONTENT_BACKUP_KEY = 'MY_S3_BUCKET_KEY'
GOOGLE_CLIENT_ID = "XXXXXXXXXXXX.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET = "XXXXXXXXXXXX"
GOOGLE_REFRESH_TOKEN = "XXXXXXXXXXXX"
GOOGLE_FOLDER_ID = 'GOOGLE_FOLDER_ID'
s3 = boto3.resource('s3')
# Get the object from the event and show its content type
bucket = CONTENT_BUCKET_NAME
key = CONTENT_BACKUP_KEY
file_name = key.split("/")[1]
file_path = os.path.join("/tmp/"+ file_name)
s3.Object(bucket, key).download_file(file_path)
filesize = os.path.getsize(file_path)
fname, extension = os.path.splitext(file_name)
# refresh token
access_token_url = 'https://accounts.google.com/o/oauth2/token'
headers = {"Content-Type":"application/json","X-Accept":"application/json"}
refresh_token_request = {"grant_type":"refresh_token", "client_id": GOOGLE_CLIENT_ID, "client_secret": GOOGLE_CLIENT_SECRET, "refresh_token": GOOGLE_REFRESH_TOKEN}
access_token_request = requests.post(access_token_url,headers=headers,data=json.dumps(refresh_token_request))
access_token = access_token_request.json()['access_token']
print(access_token)
# check file already exist
downloadUrl = "https://www.googleapis.com/drive/v3/files"
headers = {
'Host':'www.googleapis.com',
'Authorization': 'Bearer ' + access_token,
'Content-Type':'application/json; charset=UTF-8',
"X-Accept":"application/json"
}
qs= { "q": "'" + GOOGLE_FOLDER_ID + "' in parents and name='" + file_name + "' and trashed=false",
"supportsAllDrives": True,
"includeItemsFromAllDrives": True
}
fileExistCheck = requests.get(downloadUrl, params=qs, headers=headers)
responseJsonFiles = fileExistCheck.json()['files']
searchResponseLength = len(responseJsonFiles)
#upload_file()
mime = magic.Magic(mime=True)
mimeType = mime.from_file(file_path)
#folder_id = GOOGLE_FOLDER_ID
headers = {
'Host':'www.googleapis.com',
'Content-Length': str(filesize),
'Authorization': 'Bearer ' + access_token,
'Content-Type':'application/json; charset=UTF-8',
'X-Upload-Content-Type': mimeType,
'X-Upload-Content-Length': str(filesize)
}
with open(file_path, 'rb') as data:
file_name= os.path.basename(file_path)
metadata = {
"name": file_name,
"title": file_name,
"parents": [GOOGLE_FOLDER_ID],
'kind': 'drive#permission',
"permissionDetails": [
{
"permissionType": "file",
"role": "organizer"
}
],
}
# No file exist. Post new one.
if searchResponseLength < 1:
postUrl = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true"
r = requests.post(postUrl, data=json.dumps(metadata), headers=headers)
# data upload url
uploadUrl = r.headers['Location']
r2 = requests.post(uploadUrl, data=data, headers=headers)
# file exist. Put to override
else:
fileId = responseJsonFiles[0]['id']
metadata = {
"filename": file_name,
"name": file_name,
"title": file_name,
'kind': 'drive#permission',
"permissionDetails": [
{
"permissionType": "file",
"role": "organizer"
}
]
}
putUrl = "https://www.googleapis.com/upload/drive/v3/files/" + fileId + "?uploadType=resumable&supportsAllDrives=true"
r = requests.patch(putUrl, data=json.dumps(metadata), headers=headers)
uploadUrl = r.headers['Location']
r2 = requests.patch(uploadUrl, data=data, headers=headers)
Je pense qu'il y a d'autres améliorations, mais s'il existe un moyen plus simple de le mettre en œuvre, veuillez commenter.
Recommended Posts