本文整理匯總了Java中com.amazonaws.services.s3.model.PutObjectRequest類的典型用法代碼示例。如果您正苦於以下問題:Java PutObjectRequest類的具體用法?Java PutObjectRequest怎麽用?Java PutObjectRequest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PutObjectRequest類屬於com.amazonaws.services.s3.model包,在下文中一共展示了PutObjectRequest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: uploadFile
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Upload a file which is in the assets bucket.
*
* @param fileName File name
* @param file File
* @param contentType Content type for file
* @return
*/
public static boolean uploadFile(String fileName, File file, String contentType) {
try {
if (S3Module.amazonS3 != null) {
String bucket = S3Module.s3Bucket;
ObjectMetadata metaData = new ObjectMetadata();
if (contentType != null) {
metaData.setContentType(contentType);
}
PutObjectRequest putObj = new PutObjectRequest(bucket,
fileName, file);
putObj.setMetadata(metaData);
putObj.withCannedAcl(CannedAccessControlList.PublicRead);
S3Module.amazonS3.putObject(putObj);
return true;
} else {
Logger.error("Could not save because amazonS3 was null");
return false;
}
} catch (Exception e) {
Logger.error("S3 Upload -" + e.getMessage());
return false;
}
}
示例2: uploadToS3
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
public static void uploadToS3() {
// upload to s3 bucket
AWSCredentials awsCredentials = SkillConfig.getAWSCredentials();
AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client();
File folder = new File("c:/temp/morse/" + DOT + "/mp3/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
if (!s3Client.doesObjectExist("morseskill", DOT + "/" + file.getName())) {
PutObjectRequest s3Put = new PutObjectRequest("morseskill", DOT + "/" + file.getName(), file).withCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(s3Put);
System.out.println("Upload complete: " + file.getName());
}
else {
System.out.println("Skip as " + file.getName() + " already exists.");
}
}
}
}
示例3: uploadToAmazonS3
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
static public void uploadToAmazonS3(HttpSession session, File fileToUpload) throws S3Exception
{
try
{
AmazonS3 s3client = getS3();
String bucketName = getDownloadS3Bucket();
if(!s3client.doesBucketExist(bucketName))
SagLogger.logError(session, "Does not exist? S3 Bucket :" + bucketName);
AccessControlList acl = new AccessControlList();
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3client.putObject(new PutObjectRequest(bucketName, getAPKDownloadFilePathWithFile(fileToUpload.getName()), fileToUpload).withAccessControlList(acl));
SagLogger.logInfo(session, "Finished uploading to S3");
}
catch (Exception e)
{
SagLogger.logException(session, e);
throw new S3Exception(e);
}
}
示例4: store
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Stores the {@link InputStream} as an object in the S3 bucket.
*
* @param keyName The requested key name for the object.
* @param inStream The {@link InputStream} to write out to an object in S3.
* @param size The size of the {@link InputStream}.
* @return A {@link CompletableFuture} that will eventually contain the S3 object key.
*/
@Override
public CompletableFuture<String> store(String keyName, InputStream inStream, long size) {
final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE);
final String kmsKey = environment.getProperty(Constants.KMS_KEY_ENV_VARIABLE);
if (Strings.isNullOrEmpty(bucketName) || Strings.isNullOrEmpty(kmsKey)) {
API_LOG.warn("No bucket name is specified or no KMS key specified.");
return CompletableFuture.completedFuture("");
}
ObjectMetadata s3ObjectMetadata = new ObjectMetadata();
s3ObjectMetadata.setContentLength(size);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inStream, s3ObjectMetadata)
.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(kmsKey));
API_LOG.info("Writing object {} to S3 bucket {}", keyName, bucketName);
return actOnItem(putObjectRequest);
}
示例5: asynchronousAction
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Uses the {@link TransferManager} to upload a file.
*
* @param objectToActOn The put request.
* @return The object key in the bucket.
*/
@Override
protected String asynchronousAction(PutObjectRequest objectToActOn) {
String returnValue;
try {
Upload upload = s3TransferManager.upload(objectToActOn);
returnValue = upload.waitForUploadResult().getKey();
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new UncheckedInterruptedException(exception);
}
API_LOG.info("Successfully wrote object {} to S3 bucket {}", returnValue, objectToActOn.getBucketName());
return returnValue;
}
示例6: putObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest)
throws AmazonClientException, AmazonServiceException {
String blobName = putObjectRequest.getKey();
DigestInputStream stream = (DigestInputStream) putObjectRequest.getInputStream();
if (blobs.containsKey(blobName)) {
throw new AmazonS3Exception("[" + blobName + "] already exists.");
}
blobs.put(blobName, stream);
// input and output md5 hashes need to match to avoid an exception
String md5 = Base64.encodeAsString(stream.getMessageDigest().digest());
PutObjectResult result = new PutObjectResult();
result.setContentMd5(md5);
return result;
}
示例7: putObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
@SuppressWarnings("resource")
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
putObjectRequests.add(putObjectRequest);
S3Object s3Object = new S3Object();
s3Object.setBucketName(putObjectRequest.getBucketName());
s3Object.setKey(putObjectRequest.getKey());
if (putObjectRequest.getFile() != null) {
try {
s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile()));
} catch (FileNotFoundException e) {
throw new AmazonServiceException("Cannot store the file object.", e);
}
} else {
s3Object.setObjectContent(putObjectRequest.getInputStream());
}
objects.add(s3Object);
PutObjectResult putObjectResult = new PutObjectResult();
putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
return putObjectResult;
}
示例8: store
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
@Override
public void store(BuildCacheKey key, BuildCacheEntryWriter writer) {
logger.info("Start storing cache entry '{}' in S3 bucket", key.getHashCode());
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(BUILD_CACHE_CONTENT_TYPE);
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
writer.writeTo(os);
meta.setContentLength(os.size());
try (InputStream is = new ByteArrayInputStream(os.toByteArray())) {
PutObjectRequest request = getPutObjectRequest(key, meta, is);
if(this.reducedRedundancy) {
request.withStorageClass(StorageClass.ReducedRedundancy);
}
s3.putObject(request);
}
} catch (IOException e) {
throw new BuildCacheException("Error while storing cache object in S3 bucket", e);
}
}
示例9: createShortUrl
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* The method creates a short URL code for the given url
*
* @param url
* @return
*/
public ShortUrl createShortUrl(String url, String code) {
logger.info("storing the url {} in the bucket {}", url, this.bucket);
// Create the link for the short code
if (code == null) {
code = getObjectCode();
}
String loadFile = redirectFile.replace("REPLACE", url);
byte[] fileContentBytes = loadFile.getBytes(StandardCharsets.UTF_8);
InputStream fileInputStream = new ByteArrayInputStream(fileContentBytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/html");
metadata.addUserMetadata("url", url);
metadata.setContentLength(fileContentBytes.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucket, code, fileInputStream, metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
this.s3Client.putObject(putObjectRequest);
createDummyRecord(url, code);
return new ShortUrl(url, code);
}
示例10: createEmptyObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
private void createEmptyObject(final String bucketName, final String objectName)
throws AmazonClientException, AmazonServiceException {
final InputStream im = new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
};
final ObjectMetadata om = new ObjectMetadata();
om.setContentLength(0L);
if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) {
om.setServerSideEncryption(serverSideEncryptionAlgorithm);
}
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om);
putObjectRequest.setCannedAcl(cannedACL);
s3.putObject(putObjectRequest);
statistics.incrementWriteOps(1);
}
示例11: putObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
@Override
public PutObjectResult putObject(String bucketName, String key, String content)
throws AmazonServiceException, SdkClientException {
rejectNull(bucketName, "Bucket name must be provided");
rejectNull(key, "Object key must be provided");
rejectNull(content, "String content must be provided");
byte[] contentBytes = content.getBytes(StringUtils.UTF8);
InputStream is = new ByteArrayInputStream(contentBytes);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
metadata.setContentLength(contentBytes.length);
return putObject(new PutObjectRequest(bucketName, key, is, metadata));
}
示例12: create
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Constructs a new upload watcher and then immediately submits it to
* the thread pool.
*
* @param manager
* The {@link TransferManager} that owns this upload.
* @param transfer
* The transfer being processed.
* @param threadPool
* The {@link ExecutorService} to which we should submit new
* tasks.
* @param multipartUploadCallable
* The callable responsible for processing the upload
* asynchronously
* @param putObjectRequest
* The original putObject request
* @param progressListenerChain
* A chain of listeners that wish to be notified of upload
* progress
*/
public static UploadMonitor create(
TransferManager manager,
UploadImpl transfer,
ExecutorService threadPool,
UploadCallable multipartUploadCallable,
PutObjectRequest putObjectRequest,
ProgressListenerChain progressListenerChain) {
UploadMonitor uploadMonitor = new UploadMonitor(manager, transfer,
threadPool, multipartUploadCallable, putObjectRequest,
progressListenerChain);
Future<UploadResult> thisFuture = threadPool.submit(uploadMonitor);
// Use an atomic compareAndSet to prevent a possible race between the
// setting of the UploadMonitor's futureReference, and setting the
// CompleteMultipartUpload's futureReference within the call() method.
// We only want to set the futureReference to UploadMonitor's futureReference if the
// current value is null, otherwise the futureReference that's set is
// CompleteMultipartUpload's which is ultimately what we want.
uploadMonitor.futureReference.compareAndSet(null, thisFuture);
return uploadMonitor;
}
示例13: putObjectUsingMetadata
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
private PutObjectResult putObjectUsingMetadata(PutObjectRequest req) {
ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req);
// Wraps the object data with a cipher input stream
final File fileOrig = req.getFile();
final InputStream isOrig = req.getInputStream();
PutObjectRequest wrappedReq = wrapWithCipher(req, cekMaterial);
// Update the metadata
req.setMetadata(updateMetadataWithContentCryptoMaterial(
req.getMetadata(), req.getFile(), cekMaterial));
// Put the encrypted object into S3
try {
return s3.putObject(wrappedReq);
} finally {
cleanupDataSource(req, fileOrig, isOrig, wrappedReq.getInputStream(), log);
}
}
示例14: shouldUploadAndDownloadObject
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Stores a file in a previously created bucket. Downloads the file again and compares checksums
*
* @throws Exception if FileStreams can not be read
*/
@Test
public void shouldUploadAndDownloadObject() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile));
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, uploadFile.getName());
final InputStream uploadFileIS = new FileInputStream(uploadFile);
final String uploadHash = HashUtil.getDigest(uploadFileIS);
final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent());
uploadFileIS.close();
s3Object.close();
assertThat("Up- and downloaded Files should have equal Hashes", uploadHash,
is(equalTo(downloadedHash)));
}
示例15: shouldUploadWithEncryption
import com.amazonaws.services.s3.model.PutObjectRequest; //導入依賴的package包/類
/**
* Tests if Object can be uploaded with KMS
*/
@Test
public void shouldUploadWithEncryption() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String objectKey = UPLOAD_FILE_NAME;
s3Client.createBucket(BUCKET_NAME);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, objectKey, uploadFile);
putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF));
s3Client.putObject(putObjectRequest);
final GetObjectMetadataRequest getObjectMetadataRequest =
new GetObjectMetadataRequest(BUCKET_NAME, objectKey);
final ObjectMetadata objectMetadata = s3Client.getObjectMetadata(getObjectMetadataRequest);
assertThat(objectMetadata.getContentLength(), is(uploadFile.length()));
}