java多线程上传minio代码
时间: 2023-06-23 15:50:13 浏览: 187
以下是Java代码示例,演示如何使用Minio Java客户端库实现多线程上传到Minio:
```java
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.messages.Item;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MultiThreadedMinioUploader {
private static final String MINIO_ENDPOINT = "https://example.com";
private static final String ACCESS_KEY = "access_key_here";
private static final String SECRET_KEY = "secret_key_here";
private static final String BUCKET_NAME = "bucket_name_here";
public static void main(String[] args) throws Exception {
// Initialize Minio client
MinioClient minioClient = new MinioClient(MINIO_ENDPOINT, ACCESS_KEY, SECRET_KEY);
// Create a new bucket if it doesn't already exist
if (!minioClient.bucketExists(BUCKET_NAME)) {
minioClient.makeBucket(BUCKET_NAME);
}
// Upload multiple files using multiple threads
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
final int fileNumber = i;
executorService.submit(() -> {
String fileName = "file_" + fileNumber + ".txt";
File file = new File("/path/to/local/file/" + fileName);
try {
minioClient.putObject(BUCKET_NAME, fileName, file.getAbsolutePath());
System.out.println("File " + fileName + " uploaded successfully");
} catch (MinioException | IOException e) {
e.printStackTrace();
}
});
}
// Shutdown the executor service
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
// List all objects in the bucket
Iterable<Item> objects = minioClient.listObjects(BUCKET_NAME);
for (Item object : objects) {
System.out.println(object.objectName());
}
}
}
```
在此示例中,我们使用Minio Java客户端库初始化了一个Minio客户端,并使用`makeBucket`方法创建了一个新的存储桶。然后,我们使用多个线程上传了多个文件。使用`putObject`方法上传文件。在此示例中,我们使用5个线程并行上传10个文件。最后,我们使用`listObjects`方法列出了桶中的所有对象。
阅读全文