RammusXu / rammusxu.github.io

My profile
https://rammusxu.github.io/
2 stars 0 forks source link

AWS - 不用credentials存取S3的方法 #11

Closed RammusXu closed 8 years ago

RammusXu commented 8 years ago

注意事項

RammusXu commented 8 years ago

建立instace後,進入機器,看是否有正確權限使用s3

Command aws s3 ls s3://xxxxxxxxx.com.tw

Error messabe

A client error (PermanentRedirect) occurred when calling the ListObjects operation: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint: cdn.gundam.com.tw.s3.amazonaws.com You can fix this issue by explicitly providing the correct region location using the --region argument, the AWS_DEFAULT_REGION environment variable, or the region variable in the AWS CLI configuration file. You can get the bucket's location by running "aws s3api get-bucket-location --bucket BUCKET".

Solution

use aws s3 ls s3://xxxxxxxxx.com.tw --region REJION_NAME example: aws s3 ls s3://xxxxxxxxx.com.tw --region us-east-1

查詢region

http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

RammusXu commented 8 years ago

設定Credential的幾種方法

使用Provider Chain

AmazonS3 s3Client = new AmazonS3Client();
AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());

使用指定的方法

AmazonS3 s3Client = new AmazonS3Client(new EnvironmentVariableCredentialsProvider());

在java code寫入

BasicAWSCredentials awsCreds = new BasicAWSCredentials({access_key_id}, {secret_access_key})
AmazonS3 s3Client = new AmazonS3Client(awsCreds);

Default Credential Provider Chain 的優先順序

  1. Environment VariablesAWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. The AWS SDK for Java uses the EnvironmentVariableCredentialsProvider class to load these credentials.
  2. Java System Propertiesaws.accessKeyId and aws.secretKey. The AWS SDK for Java uses the SystemPropertiesCredentialsProvider to load these credentials.
  3. The default credential profiles file – typically located at ~/.aws/credentials (this location may vary per platform), this credentials file is shared by many of the AWS SDKs and by the AWS CLI. The AWS SDK for Java uses the ProfileCredentialsProvider to load these credentials.
  4. Instance profile credentials – these credentials can be used on EC2 instances, and are delivered through the Amazon EC2 metadata service. The AWS SDK for Java uses the InstanceProfileCredentialsProvider to load these credentials.

AWS credentials 開發環境的設定

http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html

[default]
aws_access_key_id={YOUR_ACCESS_KEY_ID}
aws_secret_access_key={YOUR_SECRET_ACCESS_KEY}

more info: Configuring the AWS Command Line Interface

如果要用變數指定credential file位置的話

On Linux, OS X or unix, use export:

export AWS_CREDENTIAL_PROFILES_FILE=path/to/credentials_file

On Windows, use set:

set AWS_CREDENTIAL_PROFILES_FILE=path/to/credentials_file

或是不使用credentiail file 改成直接用環境變數

To set these variables on Linux, OS X or unix, use export:

export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key

To set these variables on Windows, use set:

set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_key

AWSCredentialsProvider

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/auth/AWSCredentialsProvider.html

注意

RammusXu commented 8 years ago

使用Python驗證AWS EC2是否有權限存取S3

import boto3

# Let's use Amazon S3
s3 = boto3.resource('s3')

# Print out bucket names
for bucket in s3.buckets.all():
    print(bucket.name)

bucketName = 'my_domain.com.tw'
bucket = s3.Bucket(bucketName)
# 印出bucket下所有物件
for obj in bucket.objects.all():
     print(obj.key)

# 指定一個物件,取出後存成test
objName = 'bucket_obj/a_obj'
s3.Object(bucketName,objName).download_file('test')