无法从Python中的S3存储桶下载图像/代码已具有访问键

时间:2018-10-06 21:57:53

标签: python amazon-s3 boto3

对于一个项目,我需要在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)

1 个答案:

答案 0 :(得分:2)

看看您的代码,您有两个不同的 S3 对象getDerivedStateFromPropss3_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_idaws_secret_access_key对象,并按预期在session资源上使用了对象,但是您没有不要在s3上使用会话。

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

s3_client