- MinIO Quickstart Guide
- MinIO Docker 快速入门
- Minio纠删码快速入门
- 分布式MinIO快速入门
- 使用MinIO分解的HDP Spark和Hive
- 使用TLS安全的访问Minio服务
- MinIO存储桶通知指南
- MinIO Server config.json (v18) 指南
- MinIO多租户(Multi-tenant)部署指南
- MinIO Azure 网关
- MinIO GCS 网关
- MinIO NAS网关
- MinIO S3网关
- MinIO HDFS网关
- 磁盘缓存快速入门
- 如何使用Prometheus监控MinIO服务器
- Federation 快速入门指南
- KMS指南
- 选择API快速入门指南
- 压缩指南
- MinIO多用户快速入门指南
- MinIO STS快速入门指南
- MinIO部署快速入门
- 使用Docker Swarm部署MinIO
- 使用Kubernetes部署MinIO
- 使用Docker Compose部署MinIO
- MinIO客户端快速入门指南
- MinIO Client完全指南
- MinIO管理员完整指南
- 适用于Amazon S3兼容云存储的Minio JavaScript Library
- JavaScript Client API参考文档
- 适用于与Amazon S3兼容的云存储的MinIO Java SDK
- Java Client API参考文档
- 适用于与Amazon S3兼容的云存储的MinIO Python Library
- Python Client API文档
- 适用于与Amazon S3兼容云存储的MinIO Go SDK
- MinIO Go Client API文档
- 适用于与Amazon S3兼容的云存储的MinIO .NET SDK
- .NET Client API参考文档
- 用于Haskell的MinIO Client SDK
- MinIO Haskell SDK API参考
- 使用S3cmd操作MinIO Server
- 使用AWS CLI结合MinIO Server
- restic结合MinIO Server
- 将MySQL备份存储到MinIO Server
- 将MongoDB备份存储到MinIO Server
- 将PostgreSQL备份存储到MinIO Server
- 为MinIO Server设置Caddy proxy
- 为MinIO Server设置Nginx代理
- 使用fluentd插件聚合Apache日志
- Rclone结合MinIO Server
- 结合MinIO运行Deis Workflow
- 为MinIO Server设置Apache HTTP proxy
- 使用pre-signed URLs通过浏览器上传
- 如何在FreeNAS中运行MinIO
- 如何使用Cyberduck结合MinIO
- 如何使用AWS SDK for PHP操作MinIO Server
- 如何使用AWS SDK for Ruby操作MinIO Server
- 如何使用AWS SDK for Python操作MinIO Server
- 如何使用Mountain Duck结合MinIO
- 如何使用AWS SDK for Javascript操作MinIO Server
- 如何使用Træfɪk代理多个MinIO服务
- 如何使用AWS SDK for Go操作MinIO Server
- 如何使用AWS SDK for Java操作MinIO Server
- 如何使用Paperclip操作MinIO Server
- 如何使用AWS SDK for .NET操作MinIO Server
- 如何使用aws-cli调用MinIO服务端加密
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
如何使用AWS SDK for Java操作MinIO Server
aws-sdk-java
是Java语言版本的官方AWS SDK。本文我们将学习如何使用aws-sdk-java
操作MinIO Server。
1. 前提条件
从这里下载并安装MinIO Server。
2. 设置依赖
你可以参考AWS Java SDK文档下载并安装aws-java-sdk,或者使用Apache Maven来获得相应的依赖。
…
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aws.sdk.version>1.11.106</aws.sdk.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>${aws.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
…
3. 示例
使用下面的代码替换aws-java-sdk-1.11.213/samples/AmazonS3/S3Sample.java
,并且修改Endpoint
,BasicAWSCredentials
,bucketName
,uploadFileName
和keyName
成你的本地配置。
下面的示例描述的是如何使用asw-sdk-java来对MinIO Server进行上传和下载操作。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
public class S3Sample {
private static String bucketName = "testbucket";
private static String keyName = "hosts";
private static String uploadFileName = "/etc/hosts";
public static void main(String[] args) throws IOException {
AWSCredentials credentials = new BasicAWSCredentials("YOUR-ACCESSKEYID", "OUR-SECREYTACCESSKEY");
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
AmazonS3 s3Client = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:9000", Regions.US_EAST_1.name()))
.withPathStyleAccessEnabled(true)
.withClientConfiguration(clientConfiguration)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
try {
System.out.println("Uploading a new object to S3 from a file\n");
File file = new File(uploadFileName);
// 上传文件
s3Client.putObject(new PutObjectRequest(bucketName, keyName, file));
// 下载文件
GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, keyName);
S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
System.out.println("Printing bytes retrieved:");
displayTextInputStream(objectPortion.getObjectContent());
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
private static void displayTextInputStream(InputStream input) throws IOException {
// 按行读取并打印。
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(" " + line);
}
System.out.println();
}
}
4. 运行程序
ant
Buildfile: /home/ubuntu/aws-java-sdk-1.11.213/samples/AmazonS3/build.xml
run:
[java] Uploading a new object to S3 from a file
[java]
[java] Printing bytes retrieved:
[java] 127.0.0.1 localhost
[java] 127.0.1.1 minio
[java]
[java] # 对于支持IPv6的主机,会有以下几行输出。
[java] ::1 localhost ip6-localhost ip6-loopback
[java] ff02::1 ip6-allnodes
[java] ff02::2 ip6-allrouters
[java]
BUILD SUCCESSFUL
Total time: 3 seconds
5. 了解更多
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论