-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import boto3 | ||
from google.oauth2 import service_account | ||
from googleapiclient.discovery import build | ||
from googleapiclient.http import MediaIoBaseDownload | ||
import io | ||
|
||
# Configurations | ||
GDRIVE_FOLDER_ID = os.getenv("GDRIVE_FOLDER_ID") # Google Drive Folder ID | ||
GOOGLE_CREDENTIALS_JSON = os.getenv("GOOGLE_CREDENTIALS_JSON") # Path to Service Account JSON file or JSON string | ||
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID") | ||
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") | ||
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME") # Your S3 bucket name | ||
S3_UPLOAD_PATH = os.getenv("S3_UPLOAD_PATH", "isaac-s3-images/") # Destination folder in S3 | ||
|
||
# Authenticate with Google Drive | ||
def authenticate_google_drive(credentials_json): | ||
if os.path.isfile(credentials_json): | ||
credentials = service_account.Credentials.from_service_account_file( | ||
credentials_json, scopes=["https://www.googleapis.com/auth/drive.readonly"] | ||
) | ||
else: | ||
credentials = service_account.Credentials.from_service_account_info( | ||
eval(credentials_json), scopes=["https://www.googleapis.com/auth/drive.readonly"] | ||
) | ||
return build("drive", "v3", credentials=credentials) | ||
|
||
# Download files from Google Drive | ||
def download_from_drive(service, folder_id, download_path): | ||
os.makedirs(download_path, exist_ok=True) | ||
results = service.files().list( | ||
q=f"'{folder_id}' in parents and trashed=false", | ||
fields="files(id, name)" | ||
).execute() | ||
files = results.get("files", []) | ||
for file in files: | ||
file_id = file["id"] | ||
file_name = file["name"] | ||
file_path = os.path.join(download_path, file_name) | ||
print(f"Downloading {file_name}...") | ||
request = service.files().get_media(fileId=file_id) | ||
fh = io.FileIO(file_path, "wb") | ||
downloader = MediaIoBaseDownload(fh, request) | ||
done = False | ||
while not done: | ||
status, done = downloader.next_chunk() | ||
print(f"Download progress: {int(status.progress() * 100)}%") | ||
print("Download complete!") | ||
|
||
# Upload files to S3 | ||
def upload_to_s3(local_folder, bucket_name, s3_upload_path, aws_access_key_id, aws_secret_access_key): | ||
s3_client = boto3.client( | ||
"s3", | ||
aws_access_key_id=aws_access_key_id, | ||
aws_secret_access_key=aws_secret_access_key, | ||
) | ||
for root, _, files in os.walk(local_folder): | ||
for file in files: | ||
local_path = os.path.join(root, file) | ||
s3_key = os.path.join(s3_upload_path, os.path.relpath(local_path, local_folder)) | ||
print(f"Uploading {local_path} to s3://{bucket_name}/{s3_key}...") | ||
s3_client.upload_file(local_path, bucket_name, s3_key) | ||
print("Upload complete!") | ||
|
||
# Main script | ||
if __name__ == "__main__": | ||
# Define download folder | ||
DOWNLOAD_FOLDER = "downloads" | ||
|
||
# Authenticate and download from Google Drive | ||
drive_service = authenticate_google_drive(GOOGLE_CREDENTIALS_JSON) | ||
download_from_drive(drive_service, GDRIVE_FOLDER_ID, DOWNLOAD_FOLDER) | ||
|
||
# Upload to S3 | ||
upload_to_s3(DOWNLOAD_FOLDER, S3_BUCKET_NAME, S3_UPLOAD_PATH, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters