gpt4 book ai didi

java - S3 : Generating Pre-Signed URL for a given Key. [ key 可能/不存在]

转载 作者:行者123 更新时间:2023-12-01 18:05:38 25 4
gpt4 key购买 nike

当 S3 存储桶中不存在 key 时获取消息。 iam 检索该存储桶中的所有对象并将这些键与给定的搜索键进行匹配。如果可用,则返回 URL 字符串,否则返回消息“指定的键不存在”。

他们是否有其他方法可以在访问 key 时提高性能,而这在 S3 存储桶中不可用。

这是我的代码:

public class S3Objects {
static Properties props = new Properties();
static InputStream resourceAsStream;
static {
ClassLoader classLoader = new S3Objects().getClass().getClassLoader();
resourceAsStream = classLoader.getResourceAsStream("aws.properties");
try {
props.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws IOException, AmazonServiceException, AmazonClientException, InterruptedException {
AWSCredentials awsCreds = new
BasicAWSCredentials(props.getProperty("accessKey"), props.getProperty("secretKey"));
// PropertiesCredentials(resourceAsStream);
AmazonS3 s3Client = new AmazonS3Client( awsCreds );

String s3_BucketName = props.getProperty("bucketname");
String folderPath_fileName = props.getProperty("path");

//uploadObject(s3Client, s3_BucketName, folderPath_fileName);
//downloadObject(s3Client, s3_BucketName, folderPath_fileName);
//getSignedURLforS3File(s3Client, s3_BucketName, folderPath_fileName);
String url = getSingnedURLKey(s3Client, s3_BucketName, folderPath_fileName);
System.out.println("Received response:"+url);
}
// <MaxKeys>1000</MaxKeys>
private static String getSingnedURLKey(AmazonS3 s3Client, String s3_BucketName, String folderPath_fileName) {
String folderPath = folderPath_fileName.substring(0,folderPath_fileName.lastIndexOf("/"));
ObjectListing folderPath_Objects = s3Client.listObjects(s3_BucketName, folderPath);

List<S3ObjectSummary> listObjects = folderPath_Objects.getObjectSummaries();
for(S3ObjectSummary object : listObjects){
if(object.getKey().equalsIgnoreCase(folderPath_fileName)){
return getSignedURLforS3File(s3Client, s3_BucketName, folderPath_fileName);
}
}
return "The specified key does not exist.";
}

// providing pre-signed URL to access an object w/o any AWS security credentials.
// Pre-Signed URL = s3_BucketName.s3.amazonaws.com/folderPath_fileName?AWSAccessKeyId=XX&Expires=XX&Signature=XX
public static String getSignedURLforS3File(AmazonS3 s3Client, String s3_BucketName, String folderPath_fileName){
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(s3_BucketName, folderPath_fileName, HttpMethod.GET);
request.setExpiration( new Date(System.currentTimeMillis() + 1000 * 60 * 15) ); // Default 15 min

String url = s3Client.generatePresignedUrl( request ).toString();
System.out.println("Pre-Signed URL = " + url);
return url;
}

public static void uploadObject(AmazonS3 s3Client, String s3_BucketName, String folderPath_fileName)
throws AmazonServiceException, AmazonClientException, InterruptedException{
TransferManager tm = new TransferManager(s3Client);

PutObjectRequest putObjectRequest =
new PutObjectRequest(s3_BucketName, folderPath_fileName, new File("newImg.jpg"));
Upload myUpload = tm.upload( putObjectRequest );
myUpload.waitForCompletion();//block the current thread and wait for your transfer to complete.
tm.shutdownNow(); //to release the resources once the transfer is complete.
}
// When accessing a key which is not available in S3, it throws an exception The specified key does not exist.
public static void downloadObject(AmazonS3 s3Client, String s3_BucketName, String folderPath_fileName) throws IOException{
GetObjectRequest request = new GetObjectRequest(s3_BucketName,folderPath_fileName);
try{
S3Object s3object = s3Client.getObject( request );
System.out.println("Content-Type: " + s3object.getObjectMetadata().getContentType());
S3ObjectInputStream objectContent = s3object.getObjectContent();

FileUtils.copyInputStreamToFile(objectContent, new File("targetFile.jpg"));
}catch(AmazonS3Exception s3){
System.out.println("Received error response:"+s3.getMessage());
}
}

}

aws.properties

accessKey   =XXXXXXXXX
secretKey =XXXXXXXXX

bucketname =examplebucket
path =/photos/2006/February/sample.jpg

请告诉我是否有其他方法可以减少所有键的迭代次数并获取一些消息“键不存在”。

当我请求 key 来生成预签名 URL 时。如果

  • 提供 key « 返回签名的 URL。
  • key 不存在 « 消息 key 不可用。

最佳答案

使用getObjectMetadata根据给定的键快速确定对象是否存在。如果成功,则该对象存在。如果没有,请检查错误以确认它不是需要重试的暂时性错误。如果没有,则没有这样的 key 。

像您所做的那样迭代对象不仅不能扩展,而且成本也高得多,因为列表请求的每个请求的价格比获取对象或获取其元数据要高,这应该非常快。此操作向 S3 发送一个 HTTP HEAD 请求,仅当对象存在时才会返回 200 OK

但是,从设计角度来看,我认为该服务不应该真正关心该对象是否存在。为什么您会收到对不存在的对象的请求?谁在要求这个?这应该是调用者的问题——如果你为一个不存在的对象生成一个签名的 URL,那么当调用者尝试使用该 URL 时,请求将失败并出现错误……但是为一个不存在的对象生成一个签名的 URL不存在的对象是完全有效的操作。可以在实际上传对象之前对 URL 进行签名,并且只要 URL 未过期,一旦创建对象(如果稍后创建),它仍然有效。

关于java - S3 : Generating Pre-Signed URL for a given Key. [ key 可能/不存在],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645092/

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