我正在尝试将JSON对象写入S3。我已经在Eclipse IDE上设置了AWS凭证,并且它们可以与与DynamoDB交互的代码正常工作-这些凭证具有对AWS的完全访问权限。
我在这里使用AWS提供的示例代码:https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/s3/src/main/java/aws/example/s3/PutObject.java
但是由于某些原因,我不断收到以下错误:
java.lang.NoSuchFieldError: REQUIRES_LENGTH
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1708)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1680)
at com.javapapers.webservices.rest.jersey.metrics.DailyMetricsReportWriter.writeReportToS3(DailyMetricsReportWriter.java:42)
at com.javapapers.webservices.rest.jersey.metrics.MetricsServiceTest.shoutsAndSessionsForWriter(MetricsServiceTest.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:39)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:79)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:70)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
有什么想法吗?
我在这里其他地方看到需要apache http-core库,因此我添加了它,但仍然无法正常工作。
我也尝试过在方法中创建一个文件并将其传递,但这是行不通的。
我使用了一个示例,该示例将对象元数据设置为具有长度,但这也不起作用。
我没主意了!
public String writeReportToS3(DailyMetricsReport report) {
String bucket_name = "existing_bucket";
String file_path = "new_file_name";
String key_name = Paths.get(file_path).getFileName().toString();
System.out.format("Uploading %s to S3 bucket %s...\n", file_path, bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
try {
s3.putObject(bucket_name, key_name, new File(file_path));
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
return "done!";
}
答案 0 :(得分:3)
要选择单个SDK模块,请使用适用于Maven的AWS SDK for Java材料清单(BOM),这将确保您指定的模块使用相同版本的SDK,并且彼此兼容。 / p>
您现在可以从应用程序中使用的SDK中选择单个模块。因为您已经在BOM中声明了SDK版本,所以不需要为每个组件指定版本号。
<project>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.615</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
检查发生情况的简单方法:mvnw dependency:tree -Dverbose -Dincludes=com.amazonaws
在这种情况下(BOM:1.11.615
),每个组件的最高兼容版本为1.11.415
:
[INFO] +- com.amazonaws:aws-java-sdk-s3:jar:1.11.415:compile
[INFO] | +- com.amazonaws:aws-java-sdk-kms:jar:1.11.415:compile
[INFO] | +- com.amazonaws:aws-java-sdk-core:jar:1.11.415:compile
[INFO] | \- com.amazonaws:jmespath-java:jar:1.11.415:compile
[INFO] \- com.amazonaws:aws-java-sdk-dynamodb:jar:1.11.415:compile
注意。检查适用于Java版本2的AWS开发工具包:
您还可以冒险冒险并强迫自己依赖:
<project>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.615</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
</exclusion>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>jmespath-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.615</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>jmespath-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.615</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
<version>1.11.615</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>jmespath-java</artifactId>
<version>1.11.615</version>
</dependency>
</dependencies>
</project>
现在您得到:mvnw dependency:tree -Dverbose -Dincludes=com.amazonaws
[INFO] +- com.amazonaws:aws-java-sdk-s3:jar:1.11.615:compile
[INFO] +- com.amazonaws:aws-java-sdk-dynamodb:jar:1.11.615:compile
[INFO] +- com.amazonaws:aws-java-sdk-core:jar:1.11.615:compile
[INFO] +- com.amazonaws:aws-java-sdk-kms:jar:1.11.615:compile
[INFO] \- com.amazonaws:jmespath-java:jar:1.11.615:compile
...现在您应该运行一些严格的测试:(