如何将S3存储桶链接到Sagemaker笔记本

3
我会尽力帮您翻译。以下是需要翻译的内容:

我想将我的S3存储桶链接到笔记本实例,但是我无法做到:

这是我所了解的情况:

from sagemaker import get_execution_role

role = get_execution_role
bucket = 'atwinebankloadrisk'
datalocation = 'atwinebankloadrisk'

data_location = 's3://{}/'.format(bucket)
output_location = 's3://{}/'.format(bucket)

从存储桶中调用数据:

df_test = pd.read_csv(data_location/'application_test.csv')
df_train = pd.read_csv('./application_train.csv')
df_bureau = pd.read_csv('./bureau_balance.csv')

然而,我一直遇到错误并且无法继续操作。 我没有找到能够提供很大帮助的答案。
PS:我对AWS还不熟悉。

您可以将S3位置传递给您的训练作业。我从未见过在笔记本实例中可以这样做。如果您想要在笔记本内部使用S3数据,则只需通过boto3 S3客户端下载即可。 - undefined
5个回答

7
您可以使用下面的示例代码将S3数据加载到AWS SageMaker笔记本中。确保Amazon SageMaker角色附加了策略以访问S3。
[1] https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html
import boto3 
import botocore 
import pandas as pd 
from sagemaker import get_execution_role 

role = get_execution_role() 

bucket = 'Your_bucket_name' 
data_key = your_data_file.csv' 
data_location = 's3://{}/{}'.format(bucket, data_key) 

pd.read_csv(data_location) 

这个很好用,怎么没人点赞呢! pandas使用s3fs来处理S3文件,来源:https://dev59.com/zloT5IYBdhLWcg3w0B-_ - undefined

3

您正在尝试使用Pandas从S3中读取文件 - Pandas可以从本地磁盘读取文件,但不能直接从S3读取。
相反,请将文件从S3下载到本地磁盘上,然后使用Pandas来读取它们。

import boto3
import botocore

BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = 'my_image_in_s3.jpg' # replace with your object key

s3 = boto3.resource('s3')

try:
    # download as local file
    s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')

    # OR read directly to memory as bytes:
    # bytes = s3.Object(BUCKET_NAME, KEY).get()['Body'].read() 
except botocore.exceptions.ClientError as e:
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise

3
我希望能够在SageMaker笔记本实例中直接读取S3存储桶内的内容,而无需将其下载到硬盘。你能提供帮助吗? - undefined
@AtwineMugume bytes = s3.Object(bucket, key).get()['Body'].read() - undefined

2
在Pandas 1.0.5中,如果您已经提供了对笔记本实例的访问权限,则从S3读取CSV文件就像这样简单(https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#reading-remote-files):
df = pd.read_csv('s3://<bucket-name>/<filepath>.csv')

在笔记本设置过程中,我将SageMakerFullAccess策略附加到笔记本实例上,使其可以访问S3存储桶。您也可以通过IAM管理控制台执行此操作。
如果您需要凭据,有三种方法可以提供它们(https://s3fs.readthedocs.io/en/latest/#credentials):
  • aws_access_key_idaws_secret_access_keyaws_session_token环境变量
  • 配置文件,例如~/.aws/credentials
  • 对于EC2上的节点,使用IAM元数据提供程序

1
你可以使用https://s3fs.readthedocs.io/en/latest/直接读取S3文件并与Pandas一起使用。下面的代码来自这里
import os
import pandas as pd
from s3fs.core import S3FileSystem

os.environ['AWS_CONFIG_FILE'] = 'aws_config.ini'

s3 = S3FileSystem(anon=False)
key = 'path\to\your-csv.csv'
bucket = 'your-bucket-name'

df = pd.read_csv(s3.open('{}/{}'.format(bucket, key), mode='rb'))

0
import boto3

# files are referred as objects in S3.  
# file name is referred as key name in S3

def write_to_s3(filename, bucket_name, key):
    with open(filename,'rb') as f: # Read in binary mode
        return boto3.Session().resource('s3').Bucket(bucket).Object(key).upload_fileobj(f)

# Simple call the write_to_s3 function with required argument  

write_to_s3('file_name.csv', 
            bucket_name,
            'file_name.csv')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接