Skip to content

Instantly share code, notes, and snippets.

@rafael-leo-almeida
Last active December 28, 2015 17:10
Show Gist options
  • Select an option

  • Save rafael-leo-almeida/cb7c2a95a86b400c73ce to your computer and use it in GitHub Desktop.

Select an option

Save rafael-leo-almeida/cb7c2a95a86b400c73ce to your computer and use it in GitHub Desktop.
package com.template;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import io.minio.MinioClient;
import io.minio.errors.ClientException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.43-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.10.43-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>0.2.6</version>
</dependency>
* * /
public class S3Upload {
String host = "";
String accessKey = "";
String secretKey = "";
File file = new File("");
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
public void minioUpload() throws IOException, ClientException {
MinioClient s3Client = new MinioClient(host, accessKey, secretKey);
System.out.println(new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime()));
System.out.println(" - Uploading " + file.length() + " bytes");
s3Client.putObject("storage", "newDir/" + file.getName(), null, file.length(), new FileInputStream(file));
System.out.println(new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime()));
}
public void minioLoopUpload() throws IOException, ClientException {
MinioClient s3Client = new MinioClient(host, accessKey, secretKey);
for (int i = 40; i < 50; i++) {
System.out.println(
new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime())
+ " - Uploading " + file.length() + " bytes"
);
s3Client.putObject("teste", i + file.getName(), null, file.length(), new FileInputStream(file));
System.out.println(new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}
public void awsUpload() throws Exception {
try {
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
S3ClientOptions s3ClientOptions = new S3ClientOptions();
s3ClientOptions.withPathStyleAccess(true);
s3ClientOptions.disableChunkedEncoding();
AmazonS3 s3Client = new AmazonS3Client(credentials, clientConfiguration);
s3Client.setEndpoint(host);
s3Client.setS3ClientOptions(s3ClientOptions);
TransferManager tm = new TransferManager(s3Client);
Upload upload = tm.upload("teste", "MongoChef.dmg", file);
if (upload.isDone() == false) {
System.out.println("Transfer: " + upload.getDescription());
System.out.println(" - State: " + upload.getState());
System.out.println(" - Progress: " + upload.getProgress().getBytesTransferred());
}
upload.waitForCompletion();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public void awsSingleUpload() throws Exception {
try {
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
AmazonS3 s3Client = new AmazonS3Client(credentials, clientConfiguration);
s3Client.setEndpoint(host);
s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true).disableChunkedEncoding());
s3Client.putObject(new PutObjectRequest("teste", file.getName(), file));
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment