0

遵循本教程:https ://www.usgs.gov/media/files/landsat-cloud-direct-access-requester-pays-tutorial

import boto3
import rasterio as rio
from matplotlib.pyplot import imshow
from rasterio.session import AWSSession

s3 = boto3.client('s3', aws_access_key_id=AWS_KEY_ID,
                  aws_secret_access_key=AWS_SECRET)

resources = boto3.resource('s3', aws_access_key_id=AWS_KEY_ID,
                           aws_secret_access_key=AWS_SECRET)

aws_session = AWSSession(boto3.Session())

cog = 's3://usgs-landsat/collection02/level-2/standard/oli-tirs/2020/026/027/LC08_L2SP_026027_20200827_20200906_02_T1/LC08_L2SP_026027_20200827_20200906_02_T1_SR_B2.TIF'

with rio.Env(aws_session):
    with rio.open(cog) as src:
        profile = src.profile
        arr = src.read(1)
imshow(arr)

我收到以下错误:

rasterio.errors.RasterioIOError: '/vsis3/usgs-landsat/collection02/level-2/standard/oli-tirs/2020/026/027/LC08_L2SP_026027_20200827_20200906_02_T1/LC08_L2SP_026027_20200827_20200906_02_T1_SR_B2.TIF' does not exist in the file system, and is not recognized as a supported dataset name.
在 AWS CloudShell 中,如果我运行:``` aws s3 ls s3://usgs-landsat/collection02/level-2/standard/oli-tirs/2020/026/027/LC08_L2SP_026027_20200827_20200906_02_T1/ ```

我得到:

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied

我在 EC2 实例中运行 cloudshell 命令,同样的错误。

我需要在文档中指定我是请求者的权利,这有效:

aws s3 ls s3://usgs-landsat/collection02/level-2/standard/oli-tirs/2020/026/027/LC08_L2SP_026027_20200827_20200906_02_T1/ --request-payer requ
ester

使用 boto3 仍然不起作用。

我对运行 boto3 的用户拥有管理员权限。在 CloudShell 中出现与 boto 用户和 root 相同的错误。我以前使用过访问密钥和密钥,它可以很好地从“landsat-pds”存储桶(只有 L8 图像)和“sentinel-s2-l1c”存储桶下载。似乎只有“usgs-landsat”存储桶有问题(https://registry.opendata.aws/usgs-landsat/

还尝试使用 s3.list_objects 访问 usgs-landsat 存储桶:

landsat = resources.Bucket("usgs-landsat")
all_objects = s3.list_objects(Bucket = 'usgs-landsat')

得到一个类似的错误:

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

在查看其他解决方案后,一些用户发现:

os.environ["AWS_REQUEST_PAYER"] = "requester"
os.environ["CURL_CA_BUNDLE"] = "/etc/ssl/certs/ca-certificates.crt"

为了解决他们的问题,它对我没有用。

4

2 回答 2

0

正如您正确指出的那样,usgs-landsatS3 存储桶是请求者付费的,因此您需要rasterio正确配置才能处理它。

正如您在此处看到的,rasterio.session.AWSSession有一个requester_pays您可以设置的参数True来执行此操作。

我还可以指出以下几行:

s3 = boto3.client('s3', aws_access_key_id=AWS_KEY_ID,
                  aws_secret_access_key=AWS_SECRET)

resources = boto3.resource('s3', aws_access_key_id=AWS_KEY_ID,
                           aws_secret_access_key=AWS_SECRET)

不需要在您的代码片段中,因为您以后不重用s3andresources变量。

事实上,如果您的凭据正确地位于您的~/.aws/文件夹中 - 这可以通过运行python 包aws configure提供的命令行实用程序来完成(请参阅文档) - 您根本不需要导入,它会为您完成。awscliboto3rasterio

因此,您的代码片段可以修改为:

import rasterio as rio
from matplotlib.pyplot import imshow
from rasterio.session import AWSSession

aws_session = AWSSession(requester_pays=True)

cog = 's3://usgs-landsat/collection02/level-2/standard/oli-tirs/2020/026/027/LC08_L2SP_026027_20200827_20200906_02_T1/LC08_L2SP_026027_20200827_20200906_02_T1_SR_B2.TIF'


with rio.Env(aws_session):
    with rio.open(cog) as src:
        profile = src.profile
        arr = src.read(1)
imshow(arr)

在我的机器上正确运行。

于 2021-06-03T14:53:03.920 回答
-1

这对我有用

s3sr = boto3.resource('s3')
bucket='usgs-landsat'
prefix = 'collection02/'
keys_list = []
paginator = s3sr.meta.client.get_paginator('list_objects_v2')
for page in  paginator.paginate(Bucket=bucket, Prefix=prefix, Delimiter='/', RequestPayer='requester'):
    keys = [content['Key'] for content in page.get('Contents')]
    keys_list.extend(keys)
len(keys_list)

# keys_list 
['collection02/catalog.json',
 'collection02/landsat-c2l1.json',
 'collection02/landsat-c2l2-sr.json',
 'collection02/landsat-c2l2-st.json',
 'collection02/landsat-c2l2alb-bt.json',
 'collection02/landsat-c2l2alb-sr.json',
 'collection02/landsat-c2l2alb-st.json',
 'collection02/landsat-c2l2alb-ta.json']

# getting the catalog.json
response = boto3.client('s3').get_object(Bucket=bucket, Key='collection02/catalog.json', RequestPayer='requester')
jsondata = response['Body'].read().decode()
于 2021-06-03T01:54:30.163 回答