Amazon AWS Cognito 和 Python Boto3 建立 AWS 连接并将文件上传到 Bucket
Amazon AWS Cognito and Python Boto3 to establish AWS connection and upload file to Bucket
我正在尝试使用 AWS Cognito 服务来验证和上传文件。我已获得我的 regionType、identityPool、AWS 账户 ID 和 UnAuthRole。我也知道生产和开发桶名称。
我想我正在设置 AWS Access Key 和 AWS Secret Key...我想使用 cognito 进行身份验证并使用结果允许我进行存储桶列表,然后再上传文件。
我做错了什么?如何使用 cognito id 建立 S3 连接?
这是我的代码和产生的错误:
#!/usr/bin/python
import boto3
import boto
#boto.set_stream_logger('foo')
import json
client = boto3.client('cognito-identity','us-east-1')
resp = client.get_id(AccountId='<ACCNTID>',IdentityPoolId='<IDPOOLID>')
print "\nIdentity ID: %s"%(resp['IdentityId'])
print "\nRequest ID: %s"%(resp['ResponseMetadata']['RequestId'])
resp = client.get_open_id_token(IdentityId=resp['IdentityId'])
token = resp['Token']
print "\nToken: %s"%(token)
print "\nIdentity ID: %s"%(resp['IdentityId'])
resp = client.get_credentials_for_identity(IdentityId=resp['IdentityId'])
secretKey = resp['Credentials']['SecretKey']
accessKey = resp['Credentials']['AccessKeyId']
print "\nSecretKey: %s"%(secretKey)
print "\nAccessKey ID: %s"%(accessKey)
print resp
conn = boto.connect_s3(aws_access_key_id=accessKey,aws_secret_access_key=secretKey,debug=0)
print "\nConnection: %s"%(conn)
for bucket in conn.get_all_buckets():
print bucket.name
错误:
Traceback (most recent call last):
File "./test.py", line 32, in <module>
for bucket in conn.get_all_buckets():
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 440, in get_all_buckets
response.status, response.reason, body)
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><AWSAccessKeyId>ASIAILXMPZEMJAVZN7TQ</AWSAccessKeyId><RequestId>10631ACFF95610DD</RequestId><HostId>PGWDRBmhLjjv8Ast8v6kVHOG3xR8erJRV2ob3/2RmqHXwrg8HCZV578YsNLaoL24Hknr+nh033U=</HostId></Error>
对应的 iOS 代码工作正常:
AWSCognitoCredentialsProvider *credentialsProvider =
[AWSCognitoCredentialsProvider credentialsWithRegionType:awsCognitoRegionType
accountId:awsAccountId
identityPoolId:awsCognitoIdentityPool
unauthRoleArn:unauthRoleArn
authRoleArn:nil];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:awsCognitoRegionType
credentialsProvider:credentialsProvider];
....
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = [ELEEnvironment currentEnvironment].userDataS3Bucket;
uploadRequest.key = key;
uploadRequest.body = uploadFileURL;
[[self uploadTask:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]...
感谢您的帮助!
这是你的错误:
File "./test.py", line 32, in <module>
bucket = conn.get_bucket("elektradevbucket")
这是引用存储桶的代码部分:
bucket = conn.get_bucket("testbucket")
'''
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
s3.Bucket('testbucket')
您确定您 运行 或调用了正确的脚本吗?
最好的,
-尤利安
这个问题确实无效,因为身份验证不是在创建会话时失败,而是在尝试列出存储桶时失败。
从特定存储桶上传和下载上述代码可以正常工作,但不能列出所有存储桶。
# Upload a new file
data = open('test.jpg', 'rb')
s3.Bucket('mybucket').put_object(Key='test.jpg', Body=data)
# S3 Object
obj = s3.Object(bucket_name='mybucket', key='test.jpg')
response = obj.get()
data = response['Body'].read()
print len(data)
PhilBot,我不知道为什么您的原始代码示例使用 boto(而不是 boto3)连接到 s3。该代码使用 boto3 连接到 cognito。到目前为止,boto3 是稳定的,可能没有太多理由再使用 boto。 (也许当你最初发布你的问题时,boto3 并没有像今天这样稳定。)
当我尝试使用您的代码通过 boto3 连接到 kinesis 时,它不起作用——我必须将响应["Credentials"]["SessionToken"] 作为 aws_session_token 到 client() 函数。
我正在尝试使用 AWS Cognito 服务来验证和上传文件。我已获得我的 regionType、identityPool、AWS 账户 ID 和 UnAuthRole。我也知道生产和开发桶名称。
我想我正在设置 AWS Access Key 和 AWS Secret Key...我想使用 cognito 进行身份验证并使用结果允许我进行存储桶列表,然后再上传文件。
我做错了什么?如何使用 cognito id 建立 S3 连接?
这是我的代码和产生的错误:
#!/usr/bin/python
import boto3
import boto
#boto.set_stream_logger('foo')
import json
client = boto3.client('cognito-identity','us-east-1')
resp = client.get_id(AccountId='<ACCNTID>',IdentityPoolId='<IDPOOLID>')
print "\nIdentity ID: %s"%(resp['IdentityId'])
print "\nRequest ID: %s"%(resp['ResponseMetadata']['RequestId'])
resp = client.get_open_id_token(IdentityId=resp['IdentityId'])
token = resp['Token']
print "\nToken: %s"%(token)
print "\nIdentity ID: %s"%(resp['IdentityId'])
resp = client.get_credentials_for_identity(IdentityId=resp['IdentityId'])
secretKey = resp['Credentials']['SecretKey']
accessKey = resp['Credentials']['AccessKeyId']
print "\nSecretKey: %s"%(secretKey)
print "\nAccessKey ID: %s"%(accessKey)
print resp
conn = boto.connect_s3(aws_access_key_id=accessKey,aws_secret_access_key=secretKey,debug=0)
print "\nConnection: %s"%(conn)
for bucket in conn.get_all_buckets():
print bucket.name
错误:
Traceback (most recent call last):
File "./test.py", line 32, in <module>
for bucket in conn.get_all_buckets():
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 440, in get_all_buckets
response.status, response.reason, body)
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><AWSAccessKeyId>ASIAILXMPZEMJAVZN7TQ</AWSAccessKeyId><RequestId>10631ACFF95610DD</RequestId><HostId>PGWDRBmhLjjv8Ast8v6kVHOG3xR8erJRV2ob3/2RmqHXwrg8HCZV578YsNLaoL24Hknr+nh033U=</HostId></Error>
对应的 iOS 代码工作正常:
AWSCognitoCredentialsProvider *credentialsProvider =
[AWSCognitoCredentialsProvider credentialsWithRegionType:awsCognitoRegionType
accountId:awsAccountId
identityPoolId:awsCognitoIdentityPool
unauthRoleArn:unauthRoleArn
authRoleArn:nil];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:awsCognitoRegionType
credentialsProvider:credentialsProvider];
....
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = [ELEEnvironment currentEnvironment].userDataS3Bucket;
uploadRequest.key = key;
uploadRequest.body = uploadFileURL;
[[self uploadTask:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]...
感谢您的帮助!
这是你的错误:
File "./test.py", line 32, in <module>
bucket = conn.get_bucket("elektradevbucket")
这是引用存储桶的代码部分:
bucket = conn.get_bucket("testbucket")
'''
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
s3.Bucket('testbucket')
您确定您 运行 或调用了正确的脚本吗?
最好的, -尤利安
这个问题确实无效,因为身份验证不是在创建会话时失败,而是在尝试列出存储桶时失败。
从特定存储桶上传和下载上述代码可以正常工作,但不能列出所有存储桶。
# Upload a new file
data = open('test.jpg', 'rb')
s3.Bucket('mybucket').put_object(Key='test.jpg', Body=data)
# S3 Object
obj = s3.Object(bucket_name='mybucket', key='test.jpg')
response = obj.get()
data = response['Body'].read()
print len(data)
PhilBot,我不知道为什么您的原始代码示例使用 boto(而不是 boto3)连接到 s3。该代码使用 boto3 连接到 cognito。到目前为止,boto3 是稳定的,可能没有太多理由再使用 boto。 (也许当你最初发布你的问题时,boto3 并没有像今天这样稳定。)
当我尝试使用您的代码通过 boto3 连接到 kinesis 时,它不起作用——我必须将响应["Credentials"]["SessionToken"] 作为 aws_session_token 到 client() 函数。