![](/img/trans.png)
[英]How to move an image file from amazon s3 to amazon ec2 and run the instance using java
[英]Amazon S3 does not return a response or throw error when running on EC2 instance
我正在嘗試使用訪問密鑰和密鑰憑據連接到 S3 存儲桶。 這在我的本地機器上正常工作。 但是,當我嘗試在 EC2 實例上運行它時,執行似乎停止在 line result = s3Client.listObjectsV2(request);
. 沒有例外。 根本沒有回應。 我真的很感激任何幫助。
Java代碼
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accesskey, secretkey)))
.withRegion(region).build();
ListObjectsV2Result result = null;
List<S3ObjectSummary> objects = null;
String continuationToken = null;
System.out.println("Starting loop to request information");
int count = 1;
do {
ListObjectsV2Request request = new ListObjectsV2Request();
request.setBucketName(bucket);
request.setContinuationToken(continuationToken);
System.out.println("Placing request information #" + count);
result = s3Client.listObjectsV2(request);
System.out.println("Got response for request #" + count++);
continuationToken = result.getNextContinuationToken();
objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
System.out.println(os.getKey());
}
} while (continuationToken != null);
pom.xml
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.466</version>
</dependency>
S3 存儲桶策略
{
"Version": "2012-10-17",
"Id": "Policy1563965234895",
"Statement": [
{
"Sid": "Stmt1563965231235",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::xxxxxxxxxxxx:user/xyz_dev",
]
},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::xxxx-yyy-bucket",
"arn:aws:s3:::xxxx-yyy-bucket/*"
]
}
]
}
感謝您的回復。 我的代碼有多個問題(這不是 Amazon S3 的問題)
java.lang.NoSuchFieldError: SIGNING_REGION
但只發生在 EC2 上。 它沒有在代碼周圍的 try-catch 塊中捕獲,而是在 HTTP 響應中。hadoop-aws
,它有自己的 aws-sdk 版本使固定:
添加了單獨的 aws-sdk 模塊條目,而不是完整的 aws-java-sdk com.amazonaws aws-java-sdk-cognitoidp
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-cognitoidentity --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-cognitoidentity</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-cognitoidp --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-cognitoidp</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-kms --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-kms</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> </dependency>
向 hadoop-aws 添加排除項
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-aws</artifactId> <version>3.1.1</version> <exclusions> <exclusion> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-bundle</artifactId> </exclusion> </exclusions> </dependency>
您提到您的項目是 Spring BOOT 項目。
我們正在編寫一份文檔,該文檔將向您展示如何編寫調用 AWS 服務的 Spring BOOT 應用程序(在文檔中,以 DynamoDB 為例)並將其部署到 AWS Elastic Beanstalk。
當您這樣做時,您需要做一些事情才能使 Spring BOOT 應用程序正常工作,例如:
通過添加名為SERVER_PORT的新環境變量(值為 5000)來設置 Spring Boot 偵聽的端口。
添加一個名為AWS_ACCESS_KEY_ID的新變量並指定您的訪問密鑰值。
添加一個名為AWS_SECRET_ACCESS_KEY的新變量並指定您的密鑰值。
要創建 AWS 服務客戶端,請使用EnvironmentVariableCredentialsProvider - 像這樣使用環境變量。
Region region = Region.US_EAST_1; DynamoDbClient ddb =
DynamoDbClient.builder()
.region(region)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
文件完成后,我會在這里發布。
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.