AWS S3 和 Sagemaker:没有这样的文件或目录

AWS S3 and Sagemaker: No such file or directory

我创建了一个 S3 存储桶 'testshivaproject' 并在其中上传了一张图片。当我尝试在 sagemaker notebook 中访问它时,它会抛出错误 'No such file or directory'.

# import libraries
import boto3, re, sys, math, json, os, sagemaker, urllib.request
from sagemaker import get_execution_role
import numpy as np                                   

# Define IAM role
role = get_execution_role()

my_region = boto3.session.Session().region_name # set the region of the instance

print("success :"+my_region)

输出: 成功:us-east-2

role

输出: 'arn:aws:iam::847047967498:role/service-role/AmazonSageMaker-ExecutionRole-20190825T121483'

bucket = 'testprojectshiva2' 
data_key = 'ext_image6.jpg' 
data_location = 's3://{}/{}'.format(bucket, data_key) 
print(data_location)

输出: s3://testprojectshiva2/ext_image6.jpg

test = load_img(data_location)

输出:没有那个文件或目录

有类似问题提出()但没有找到解决方法?

感谢使用 Amazon SageMaker!

我从你的描述中猜到了,但你是否尝试使用 Keras load_img 函数直接从你的 S3 存储桶加载图像?

不幸的是,the load_img function is designed to only load files from disk,因此将 s3:// URL 传递给该函数将始终 return 一个 FileNotFoundError.

在使用之前先从 S3 下载图像是很常见的,因此您可以在调用 load_img.

之前使用 boto3 或 AWS CLI 下载文件

或者,由于load_img函数只是创建了一个PIL Image对象,你可以使用boto3直接从S3中的数据创建PIL对象,并且根本不使用 load_img 函数。

换句话说,你可以这样做:

from PIL import Image

s3 = boto3.client('s3')
test = Image.open(BytesIO(
    s3.get_object(Bucket=bucket, Key=data_key)['Body'].read()
    ))

希望这对您的项目有所帮助!

您可以使用以下代码将 CSV 文件导入 sagemaker。

import pandas as pd

bucket='your-s3-bucket'
data_key = 'your.csv'
data_location = 's3://{}/{}'.format(bucket, data_key)
df = pd.read_csv(data_location)

data_location 变量的替代格式:

data_location = f's3://{bucket}/{data_key}'