gpt4 book ai didi

java : Use Server-Side Encryption in Amazon S3 using vfs s3 plugin

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:42 27 4
gpt4 key购买 nike

为了在 S3 中复制文件,我使用的是 vfs-s3-2.2.1.jar我在 com.intridea.io.vfs.provider.s3 包下找到了 S3FileObject 类。我在其中使用 public void copyFrom(final FileObject file, final FileSelector selector) 方法复制文件。在这种方法中,我发现了以下代码:

try {
if (srcFile.getType().hasChildren()) {
destFile.createFolder();
// do server side copy if both source and dest are in S3 and using same credentials
} else if (srcFile instanceof S3FileObject) {
S3FileObject s3SrcFile = (S3FileObject)srcFile;
String srcBucketName = s3SrcFile.getBucket().getName();
String srcFileName = s3SrcFile.getS3Key();
String destBucketName = destFile.getBucket().getName();
String destFileName = destFile.getS3Key();
CopyObjectRequest copy = new CopyObjectRequest(
srcBucketName, srcFileName, destBucketName, destFileName);
if (srcFile.getType() == FileType.FILE && getServerSideEncryption()) {
ObjectMetadata meta = s3SrcFile.getObjectMetadata();
meta.setSSEAlgorithm(AES_256_SERVER_SIDE_ENCRYPTION);
copy.setNewObjectMetadata(meta);
}
getService().copyObject(copy);
} else if (srcFile.getType().hasContent() && srcFile.getURL().getProtocol().equals("file")) {
// do direct upload from file to avoid overhead of making a copy of the file
try {
File localFile = new File(srcFile.getURL().toURI());
destFile.upload(localFile);
} catch (URISyntaxException e) {
// couldn't convert URL to URI, but should still be able to do the slower way
super.copyFrom(file, selector);
}
} else {
super.copyFrom(file, selector);
}
} catch (IOException e) {
throw new FileSystemException("vfs.provider/copy-file.error", new Object[]{srcFile, destFile}, e);
} catch (AmazonClientException e) {
throw new FileSystemException("vfs.provider/copy-file.error", new Object[]{srcFile, destFile}, e);
} finally {
destFile.close();
}

In official reference it uses these method

withSourceSSECustomerKey(sseKey)
withDestinationSSECustomerKey(newSseKey);

vfs-s3-2.2.1.jar S3FileObjectcopyFrom 方法中我找不到任何方法来设置 SSECustomerKey我怎样才能达到同样的效果。感谢您看这里。

最佳答案

我没有测试,但我快速查看了库/代码 - 在 https://github.com/abashev/vfs-s3/blob/branch-2.3.x/src/main/java/com/intridea/io/vfs/provider/s3/S3FileSystemConfigBuilder.java 中有一种方法可以设置服务器端加密

/**
* use server-side encryption.
*
* @param opts The FileSystemOptions.
* @param serverSideEncryption true if server-side encryption should be used.
*/
public void setServerSideEncryption(FileSystemOptions opts, boolean serverSideEncryption)
{
setParam(opts, SERVER_SIDE_ENCRYPTION, serverSideEncryption);
}

所以在你调用copyFrom之前你可以做

    S3FileSystemConfigBuilder.getInstance().setServerSideEncryption(
S3FileSystemConfigBuilder.getInstance().getFileSystem().getFileSystemOptions(),
true);

关于java : Use Server-Side Encryption in Amazon S3 using vfs s3 plugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891632/

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