使用多线程将图片作为存储平台上传到AmasonS3

时间:2018-01-08 09:03:40

标签: java multithreading amazon-s3

官方API中只有单个上传和部分上传,而我写的线程池(20)上传有连接超时错误:

  

线程“pool-1-thread-12”中的异常   com.amazonaws.SdkClientException:无法执行HTTP请求:   连接到s3.amazonaws.com:80 [s3.amazonaws.com/54.231.66.16]失败:   连接超时

我看到了AmasonS3的API,但我找不到答案。

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.gaodig.stream.common.Constants;
import com.gaodig.stream.config.VEEConfig;

public class CephS3Client {

    private static volatile AmazonS3 s3Client = null;

    public static AmazonS3 getClient() {
        if (s3Client == null) {
            synchronized (CephS3Client.class) {
                if (s3Client == null) {
                    AWSCredentials credentials = new BasicAWSCredentials(Constants.CephS3AccessKey,
                            Constants.CephS3SecretKey);
                    ClientConfiguration clientConfig = new ClientConfiguration();
                    clientConfig.setProtocol(Protocol.HTTP);
                    clientConfig.setSignerOverride("S3SignerType");

                    // AmazonS3 conn = new AmazonS3Client(credentials,
                    // clientConfig);
                    s3Client = new AmazonS3Client(credentials, clientConfig);

                    s3Client.setEndpoint(VEEConfig.CEPH_ENDPOINT());
                }
            }
        }
        return s3Client;
    }

}

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,这个功能在java sdk 1.0中没有实现。但是在新的amazon java sdk中,他们为dynamo db和s3实现了很多异步功能,同样也用于http客户端。所以现在您可以为s3使用async客户端。示例:

public static void main(String[] args) {
S3AsyncClient client = S3AsyncClient.create();
CompletableFuture<PutObjectResponse> future = client.putObject(
        PutObjectRequest.builder()
                        .bucket(BUCKET)
                        .key(KEY)
                        .build(),
        AsyncRequestProvider.fromFile(Paths.get("myfile.in"))
);
future.whenComplete((resp, err) -> {
    try {
        if (resp != null) {
            System.out.println(resp);
        } else {
            // Handle error
            err.printStackTrace();
        }
    } finally {
        // Lets the application shut down. Only close the client when you are completely done with it.
        FunctionalUtils.invokeSafely(client::close);
    }
});

}

您可以阅读此文档new AWS SDK for Java或许可以帮助您