Skip to content

Instantly share code, notes, and snippets.

@uzresk
Created November 10, 2014 07:02
Show Gist options
  • Save uzresk/3b03d1c500f380f017d7 to your computer and use it in GitHub Desktop.
Save uzresk/3b03d1c500f380f017d7 to your computer and use it in GitHub Desktop.
S3 Fileupload(Single/Multipart)[SDK for Java] ref: http://qiita.com/uzresk/items/47eb891dc65a0773172b
package aws.s3.upload;
import java.io.File;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.event.ProgressEvent;
import com.amazonaws.event.ProgressEventType;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerConfiguration;
import com.amazonaws.services.s3.transfer.TransferProgress;
import com.amazonaws.services.s3.transfer.Upload;
public class S3MultiparatUpload {
public static void main(String[] args) {
AWSCredentialsProvider provider = new ProfileCredentialsProvider("uzr");
TransferManager tm = new TransferManager(provider.getCredentials());
File file = new File("/Users/yuzuru/Downloads/10mfile");
long start = System.currentTimeMillis();
Upload upload = tm.upload(new PutObjectRequest("data.uzr", "10mfile",
file));
// Listnerを仕込みます。
upload.addProgressListener(new ProgressListener() {
/**
* 進捗状況が変わったら呼ばれるメソッド
* ProgressEventからは、転送バイト数やパーセンテージなどが取得できます。
* ここでは転送完了後メッセージを出すようにしています。
*/
@Override
public void progressChanged(ProgressEvent progressEvent) {
if (ProgressEventType.TRANSFER_COMPLETED_EVENT == progressEvent.getEventType()) {
long end = System.currentTimeMillis();
long interval = end - start;
System.out.println("処理時間:" + interval + "ミリ秒");
System.out.println("upload finish.");
}
}
});
}
}
package aws.s3.upload;
import java.io.File;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class S3SingleUpload {
public static void main(String[] args) {
AWSCredentialsProvider provider = new ProfileCredentialsProvider("uzr");
// s3clientインスタンスを作成
AmazonS3 s3client = new AmazonS3Client(provider.getCredentials());
File file = new File("/Users/yuzuru/Downloads/10mfile");
long start = System.currentTimeMillis();
// アップロードします。
// PutObjectRequestの引数はバケット名、アップロード後の名前、Fileオブジェクトです。
s3client.putObject(new PutObjectRequest("data.uzr", "10mfile",file));
// 処理時間を取得
long end = System.currentTimeMillis();
long interval = end - start;
System.out.println("処理時間:" + interval + "ミリ秒");
System.out.println("upload finish.");
}
}
TransferManager tm = new TransferManager(provider.getCredentials());
TransferManagerConfiguration configuration = new TransferManagerConfiguration();
configuration.setMinimumUploadPartSize(5 * MB);
configuration.setMultipartUploadThreshold(5 * GB);
tm.setConfiguration(configuration);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment