我是亞馬遜s3 web服務中的新成員,需要開發一個命令行應用程序來在Amazon S3存儲桶之間傳輸文件。輸入文件的內容必須轉換爲目標格式,然後複製到目標文件夾。目標格式可以是XML或Json,文件內容可以遵循給定的數據模型。如何使用java以編程方式在Amazon S3存儲桶之間傳輸文件?
我對Java有中介經驗,並且剛剛創建了一個尚未解決的帳戶,因此試圖開發一個工作流程來解決問題。
我是亞馬遜s3 web服務中的新成員,需要開發一個命令行應用程序來在Amazon S3存儲桶之間傳輸文件。輸入文件的內容必須轉換爲目標格式,然後複製到目標文件夾。目標格式可以是XML或Json,文件內容可以遵循給定的數據模型。如何使用java以編程方式在Amazon S3存儲桶之間傳輸文件?
我對Java有中介經驗,並且剛剛創建了一個尚未解決的帳戶,因此試圖開發一個工作流程來解決問題。
好吧,這並不難。幾個月前,我已經給客戶做過了,您可能會在下面找到代碼。要從AmazonS3存儲桶讀取文件,請閱讀本Amazon文檔[1]。要將文件寫入Amazon s3存儲桶,請閱讀本文檔[2]。
除此之外,您可能需要將所有訪問令牌添加到本地操作系統。你可以從管理員那裏得到一些幫助。正如我記得的,獲得正確的憑證是唯一棘手的部分。
亞馬遜有一個很好的小文件,我建議你也要經歷一下。
package org.saig.watermark.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.AmazonClientException;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
public class AmazonS3Util {
private static AWSCredentials credentials = null;
private static final String fileSeparator = "/";
private static final Log log = LogFactory.getLog(AmazonS3Util.class);
static {
/*
* The ProfileCredentialsProvider will return your [default]
* credential profile by reading from the credentials file located at
* (~/.aws/credentials).
*/
try {
credentials = new ProfileCredentialsProvider().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (~/.aws/credentials), and is in valid format.",
e);
}
}
public static void readFileFromS3cketBucket(String bucketName, String key, String dirPath,
String fileName) {
FilterInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
// Remove the file if it already exists.
if (new File(dirPath + WatermarkConstants.fileSeparator + fileName).exists()) {
FileUtil.delete(new File(dirPath + WatermarkConstants.fileSeparator + fileName));
}
AmazonS3 s3 = new AmazonS3Client(credentials);
Region usEast1 = Region.getRegion(Regions.US_EAST_1);
s3.setRegion(usEast1);
log.info("Downloading an object from the S3 bucket.");
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
log.info("Content-Type: " + object.getObjectMetadata().getContentType());
inputStream = object.getObjectContent();
File dirForOrder = new File(dirPath);
if (!dirForOrder.exists()) {
dirForOrder.mkdir();
}
outputStream = new FileOutputStream(new File(dirPath + fileSeparator + fileName));
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
public static void uploadFileToS3Bucket(String bucketName, String key, String dirPath,
String fileName) {
AmazonS3 s3 = new AmazonS3Client(credentials);
Region usEast1 = Region.getRegion(Regions.US_EAST_1);
s3.setRegion(usEast1);
s3.putObject(new PutObjectRequest(bucketName, key, new File(dirPath + fileSeparator +
fileName)));
try {
FileUtil.delete(new File(dirPath));
} catch (IOException e) {
log.error(e);
}
}
public static void main(String[] args) {
readFileFromS3cketBucket("bucketName",
"s3Key",
"localFileSystemPath",
"destinationFileName.pdf");
}
}
希望這會有所幫助。快樂編碼!
[1] http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html [2] http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html
而不是編寫自己的應用程序,你可以使用[AWS命令行界面(CLI)(http://aws.amazon.com/cli/ ),甚至創建一個調用它的腳本。 –
我寧願寫自己的應用程序,因爲它稍後會包含一些特性。 – Chak