gpt4 book ai didi

android - 在gradle构建期间检索AWS数据

转载 作者:行者123 更新时间:2023-12-03 05:04:19 27 4
gpt4 key购买 nike

我正在尝试从AWS中获取一些值(例如,从cognito或s3)。我认为适当的是,您在build.gradle中定义了一个任务,该任务在gradle运行时的构建期间执行。我遵循了文档(https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/creating-clients.html)中编写的说明,但是当我尝试使用已导入的AWS依赖项中的方法时,出现“无法解析符号”错误。

这些导入的依赖项遵循本教程中给出的示例:

...
dependencies {
implementation platform('software.amazon.awssdk:bom:2.X.X')

implementation 'software.amazon.awssdk:kinesis'
...
}

代码中的gradle中发生错误(例如,无法识别KinesisClient):
def task = {
KinesisClient kinesisClient = KinesisClient.builder()
.region(ARANDROMREGION)
.build();
}

在构建期间如何从gradle访问AWS值?这是最基本的问题。如果我提供的信息不清楚,请告诉我。

最佳答案

这是使用Java S3 API和有效的 build.gradle 文件的完整示例(V1和V2)

JAVA V1
这是S3 Java代码:

package com.amazon.s3;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.GetBucketLocationRequest;


import java.io.IOException;

public class CreateBucket {

public static void main(String[] args) throws IOException {
Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName = "My Bucket";

try {

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.withRegion(clientRegion)
.build();

if (!s3Client.doesBucketExistV2(bucketName)) {
// Because the CreateBucketRequest object doesn't specify a region, the
// bucket is created in the region specified in the client.
s3Client.createBucket(new CreateBucketRequest(bucketName));

// Verify that the bucket was created by retrieving it and checking its location.
String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
System.out.println("Bucket location: " + bucketLocation);
}
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it and returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
}
}

这是 build.gradle 文件:
plugins {
id 'java'
}

group 'AWSS3_Gra'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.228')
implementation 'com.amazonaws:aws-java-sdk-s3'
testCompile group: 'junit', name: 'junit', version: '4.11'
}

JAVA V2

这是S3 Java代码:
package com.example.s3;


import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import java.util.List;
import java.util.ListIterator;


public class ListObjects {

public static void main(String[] args) {


String bucketName = "BucketName";
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder()
.region(region)
.build();

ListBucketObjects(s3, bucketName);
}


public static void ListBucketObjects( S3Client s3, String bucketName ) {

try {
ListObjectsRequest listObjects = ListObjectsRequest
.builder()
.bucket(bucketName)
.build();

ListObjectsResponse res = s3.listObjects(listObjects);
List<S3Object> objects = res.contents();

for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
S3Object myValue = (S3Object) iterVals.next();
System.out.print("\n The name of the key is " + myValue.key());
System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
System.out.print("\n The owner is " + myValue.owner());
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
//convert bytes to kbs
private static long calKb(Long val) {
return val/1024;

}

}

构建文件是:
group 'aws.test'
version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation platform('software.amazon.awssdk:bom:2.10.30')
implementation 'software.amazon.awssdk:s3'
testImplementation group: 'junit', name: 'junit', version: '4.11'
}

希望这可以帮助...

PS-这是指向此信息的AWS DEV指南的链接: https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/setup-project-gradle.html

关于android - 在gradle构建期间检索AWS数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62133162/

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