首页 > 解决方案 > 无法从 Python 中的 S3 存储桶下载图像/代码已经有访问密钥

问题描述

对于一个项目,我需要在我的 S3 存储桶中下载一些项目。我已经看过关于这个主题的类似帖子;但是,我在程序中硬编码了我的访问密钥 ID 和秘密访问密钥,但仍然无法下载它们。Python不断返回错误:

“botocore.exceptions.NoCredentialsError:无法找到凭据”

尽管提供了我的凭据,但我仍然无法下载。下面提供了我的代码。谁能帮我纠正这个?

import boto3
import os
"""import sys
import csv
import pandas as pd
import numpy as np
import tensorflow as tf"""
import nibabel as nib
from boto3.session import Session


aws_access_key_id = '********************'
aws_secret_access_key = '****************************************'
bucket1 = 'adnimcic'
mcic = [[], [], []]
mcicc = [[], [], []]
bucket2 = 'adnimcinc'
mcinc = [[], [], []]
bucket3 = 'adniresults'
results = []
s3_client = boto3.client('s3')


#connecting to S3
session = Session(aws_access_key_id, aws_secret_access_key)
s3 = session.resource('s3')
bucket1obj = s3.Bucket(bucket1)
#bucket2obj = s3.Bucket(bucket2)
#'MCIc_Segmented/ADNI_002_S_0729_MR_MP-RAGE_REPEAT_br_raw_20070225105857428_72_S27091_I41585_be_be_pve_2.nii.gz_extracted'
def concatenate(name):
    name = name.split('.')
    name.pop()
    name = name[0] + '.' + name[1]
    name = name.split('/')
    name = name[1]
    return name

def download(bucketname, key):
    path = '/Volumes/LaCie Mac/' + concatenate(key.key)
    s3_client.download_file(bucketname, key.key, path)

for key in bucket1obj.objects.all():
    if 'pve_0' and 'extracted' in key.key:
        mcic[0].append(key)
        download(bucket1, key)
for key in bucket1obj.objects.all():
    if 'pve_1' and 'extracted' in key.key:
        mcic[1].append(key)
        download(bucket1, key)
for key in bucket1obj.objects.all():
    if 'pve_2' and 'extracted' in key.key:
        mcic[2].append(key)
        download(bucket1, key)

标签: pythonamazon-s3boto3

解决方案


查看您的代码,您有两个不同的S3对象,s3_client并且s3

s3_client = boto3.client('s3')

...

session = Session(aws_access_key_id, aws_secret_access_key)
s3 = session.resource('s3')

...

def download(bucketname, key):
    path = '/Volumes/LaCie Mac/' + concatenate(key.key)
    s3_client.download_file(bucketname, key.key, path)

```

查看您的代码,您似乎在您的对象上使用了您的aws_access_key_id和您在资源上使用的对象,但您没有在.aws_secret_access_keysessions3s3_client

如果我正确理解您的问题,您应该能够通过从会话创建客户端来解决此问题,例如:

session = Session(aws_access_key_id, aws_secret_access_key)
s3 = session.resource('s3')
s3_client = session.client('s3')

推荐阅读