gpt4 book ai didi

java - AWS Amazon S3 Java SDK - 上传大文件时过期时刷新凭证/ token

转载 作者:行者123 更新时间:2023-12-02 01:59:35 24 4
gpt4 key购买 nike

我正在尝试将一个大文件上传到使用 token 的服务器,并且 token 会在 10 分钟后过期,因此如果我上传一个小文件,它将起作用,因此如果文件太大,我会遇到一些问题,并且当访问被拒绝时将永远尝试上传

因此,我需要刷新 BasicAWSCredentials 中的 token ,该 token 用于 AWSStaticCredentialsProvider,因此我不确定如何执行此操作,请帮助 =)

值得一提的是,我们使用本地服务器(不是亚马逊云)来提供 token ,并且为了方便起见,我们使用亚马逊的代码。

这是我的代码:

public void uploadMultipart(File file) throws Exception {
//this method will give you a initial token for a given user,
//than calculates when a new token is needed and will refresh it just when necessary

String token = getUsetToken();
String existingBucketName = myTenant.toLowerCase() + ".package.upload";
String endPoint = urlAPI + "s3/buckets/";
String strSize = FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(file));
System.out.println("File size: " + strSize);

AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(endPoint, null);//note: Region has to be null
//AWSCredentialsProvider
BasicAWSCredentials sessionCredentials = new BasicAWSCredentials(token, "NOT_USED");//secretKey should be set to NOT_USED

AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(sessionCredentials))
.withEndpointConfiguration(endpointConfiguration)
.enablePathStyleAccess()
.build();

int maxUploadThreads = 5;
TransferManager tm = TransferManagerBuilder
.standard()
.withS3Client(s3)
.withMultipartUploadThreshold((long) (5 * 1024 * 1024))
.withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads))
.build();

PutObjectRequest request = new PutObjectRequest(existingBucketName, file.getName(), file);
//request.putCustomRequestHeader("Access-Token", token);
ProgressListener progressListener = progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred());
request.setGeneralProgressListener(progressListener);
Upload upload = tm.upload(request);

LocalDateTime uploadStartedAt = LocalDateTime.now();
log.info("Starting upload at: " + uploadStartedAt);

try {
upload.waitForCompletion();
//upload.waitForUploadResult();
log.info("Upload completed. " + strSize);

} catch (Exception e) {//AmazonClientException
log.error("Error occurred while uploading file - " + strSize);
e.printStackTrace();
}
}

最佳答案

找到解决方案!

我找到了一种方法来实现这一点,说实话,我对结果非常满意,我已经用大文件(50gd.zip)做了很多测试,并且在每种情况下都运行得很好

我的解决方案是,删除该行: BasicAWSCredentials sessionCredentials = new BasicAWSCredentials(token, "NOT_USED");

AWSCredentials 是一个接口(interface),因此我们可以用动态的东西覆盖它, token 过期并且需要新的新鲜 token 时的逻辑保存在< em>getToken() 方法意味着您可以每次调用而不会造成任何损害

AWSCredentials sessionCredentials = new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
try {
return getToken(); //getToken() method return a string
} catch (Exception e) {
return null;
}
}

@Override
public String getAWSSecretKey() {
return "NOT_USED";
}
};

关于java - AWS Amazon S3 Java SDK - 上传大文件时过期时刷新凭证/ token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51827310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com