我有一個boto3腳本,可以使用我的賬戶的AccessKeyId和SecretAccessKey成功將文件上傳到S3存儲桶。這工作正常。我應該用boto3寫一個只有IAM實例配置文件的S3存儲桶,但是怎麼做?
但我應該從這個實例中刪除我的憑據,並只使用附加到實例的IAM角色。我已經做了各種嘗試,但還沒有得到這個工作,一般用:
botocore.exceptions.ClientError: An error occurred (InvalidToken) when
calling the PutObject operation: The provided token is malformed or
otherwise invalid.
我的代碼:
!/usr/bin/env python
import datetime
import sys
import os
import ConfigParser
import boto3
s3 = boto3.resource('s3')
config = ConfigParser.ConfigParser()
configfile = config.read('edi.config')
s3bucket = config.get('default', 's3bucket')
s3bucket = s3bucket.strip()
print 's3bucket: ', s3bucket
today = datetime.date.today()
todaystr = today.strftime('%m_%d_%Y')
os.chdir(todaystr)
try:
os.mkdir('uploaded')
except:
pass
for f in os.listdir('.'):
if not os.path.isfile(f):
continue
print 'uploading', f
data = open(f)
s3.Bucket('ustc-submissions-non-prod').put_object(Key='closewatch/incoming/%s' % f, Body=data)
os.rename(f,'uploaded/%s' % f)
我發現一張紙條,在其他地方,我需要承擔boto3內的IAM角色,但(a)我沒有這樣做的許可,(b)我沒有權限給予我自己的許可,(c)我的同事認爲這不應該是必需的。
有人有這種事情的完整例子嗎?
沒有代碼,它有點難以猜測,但在所有可能的情況下,你都不需要傳遞任何東西。有關更多信息,請參見[boto3 credentials](http://boto3.readthedocs.io/en/latest/guide/configuration.html)。您的使用案例是第8點。 – stdunbar
您需要編輯您的帖子以包含您的代碼(減去憑證),以便我們可以看到您跌倒的位置。如果實例具有正確的IAM角色,您*絕對*不需要訪問密鑰。 –
你可以在命令行中輸入s3 cp foo.txt s3:// bucketname /'嗎? (如果沒有找到'aws'命令,'pip install awscli') –