S3にファイルをアップロード
import sagemaker, boto3, os
sagemaker_source_path = "./my_image.jpg"
s3_dest_path = "my_image.jpg"
sess = sagemaker.Session(default_bucket='backet_name')
bucket = sess.default_bucket()
boto3.Session().resource('s3').Bucket(bucket).Object(s3_dest_path).upload_file(sagemaker_source_path)
S3からファイルをダウンロード
import sagemaker, boto3, os
s3_source_path = "./my_image.jpg"
sagemaker_dest_path = "my_image.jpg"
sess = sagemaker.Session(default_bucket='backet_name')
bucket = sess.default_bucket()
boto3.Session().resource('s3').Bucket(bucket).Object(s3_source_path).download_file(sagemaker_dest_path)
GoogleDriveからS3にデータを移動
zip形式にして、gdownでダウンロード、s3にアップ。
!pip install gdown
!pip install --upgrade gdown
gdown https://drive.google.com/uc?id=********
S3のディレクトリをダウンロード
import os
import boto3
def download_directory_from_s3(bucket_name, s3_directory, local_directory):
"""
S3 からディレクトリをダウンロードする関数
"""
s3 = boto3.client('s3')
for obj in s3.list_objects(Bucket=bucket_name, Prefix=s3_directory)['Contents']:
if not os.path.exists(os.path.dirname(obj['Key'])):
os.makedirs(os.path.dirname(obj['Key']))
s3.download_file(bucket_name, obj['Key'], obj['Key'])