本文整理匯總了Java中com.amazonaws.services.s3.model.ObjectMetadata類的典型用法代碼示例。如果您正苦於以下問題:Java ObjectMetadata類的具體用法?Java ObjectMetadata怎麽用?Java ObjectMetadata使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ObjectMetadata類屬於com.amazonaws.services.s3.model包,在下文中一共展示了ObjectMetadata類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: uploadFile
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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: shouldGetObjectMetadata
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
/**
* Tests if the Metadata of an existing file can be retrieved.
*/
@Test
public void shouldGetObjectMetadata() {
final String nonExistingFileName = "nonExistingFileName";
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final ObjectMetadata metadataExisting =
s3Client.getObjectMetadata(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat("The ETags should be identically!", metadataExisting.getETag(),
is(putObjectResult.getETag()));
thrown.expect(AmazonS3Exception.class);
thrown.expectMessage(containsString("Status Code: 404"));
s3Client.getObjectMetadata(BUCKET_NAME, nonExistingFileName);
}
示例3: store
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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);
}
示例4: move
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
@Override
public void move(String sourceBlobName, String targetBlobName) throws IOException {
try {
CopyObjectRequest request = new CopyObjectRequest(blobStore.bucket(), buildKey(sourceBlobName),
blobStore.bucket(), buildKey(targetBlobName));
if (blobStore.serverSideEncryption()) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
request.setNewObjectMetadata(objectMetadata);
}
SocketAccess.doPrivilegedVoid(() -> {
blobStore.client().copyObject(request);
blobStore.client().deleteObject(blobStore.bucket(), buildKey(sourceBlobName));
});
} catch (AmazonS3Exception e) {
throw new IOException(e);
}
}
示例5: shouldCopyObjectEncrypted
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
/**
* Puts an Object; Copies that object to a new bucket; Downloads the object from the new
* bucket; compares checksums
* of original and copied object
*
* @throws Exception if an Exception occurs
*/
@Test
public void shouldCopyObjectEncrypted() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationBucket";
final String destinationKey = "copyOf/" + sourceKey;
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
copyObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF));
final CopyObjectResult copyObjectResult = s3Client.copyObject(copyObjectRequest);
final ObjectMetadata metadata =
s3Client.getObjectMetadata(destinationBucketName, destinationKey);
final InputStream uploadFileIS = new FileInputStream(uploadFile);
final String uploadHash = HashUtil.getDigest(TEST_ENC_KEYREF, uploadFileIS);
assertThat("ETag should match", copyObjectResult.getETag(), is(uploadHash));
assertThat("Files should have the same length", metadata.getContentLength(),
is(uploadFile.length()));
}
示例6: store
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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);
}
}
示例7: shouldUploadWithEncryption
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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()));
}
示例8: deleteShortUrl
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
/**
* This method deletes the url for code
*
* @param code
*/
public void deleteShortUrl(String code) {
try {
// get the object
ObjectMetadata metaData = this.s3Client.getObjectMetadata(this.bucket, code);
String url = metaData.getUserMetaDataOf("url");
logger.info("The url to be deleted {}", url);
this.s3Client.deleteObject(this.bucket, code);
this.s3Client.deleteObject(this.bucket + "-dummy", code + Base64.encodeBase64String(url.getBytes()));
} catch (AmazonS3Exception ex) {
if (ex.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return;
}
logger.warn("Unable to get object status", ex);
throw ex;
}
}
示例9: create
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
@Override
@Guarded(by = STARTED)
public Blob create(final InputStream blobData, final Map<String, String> headers) {
checkNotNull(blobData);
return create(headers, destination -> {
try (InputStream data = blobData) {
MetricsInputStream input = new MetricsInputStream(data);
TransferManager transferManager = new TransferManager(s3);
transferManager.upload(getConfiguredBucket(), destination, input, new ObjectMetadata())
.waitForCompletion();
return input.getMetrics();
} catch (InterruptedException e) {
throw new BlobStoreException("error uploading blob", e, null);
}
});
}
示例10: updateSnapshotIndex
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
/**
* Write a list of all of the state versions to S3.
* @param newVersion
*/
private synchronized void updateSnapshotIndex(Long newVersion) {
/// insert the new version into the list
int idx = Collections.binarySearch(snapshotIndex, newVersion);
int insertionPoint = Math.abs(idx) - 1;
snapshotIndex.add(insertionPoint, newVersion);
/// build a binary representation of the list -- gap encoded variable-length integers
byte[] idxBytes = buidGapEncodedVarIntSnapshotIndex();
/// indicate the Content-Length
ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("Content-Length", (long)idxBytes.length);
/// upload the new file content.
try(InputStream is = new ByteArrayInputStream(idxBytes)) {
Upload upload = s3TransferManager.upload(bucketName, getSnapshotIndexObjectName(blobNamespace), is, metadata);
upload.waitForCompletion();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
示例11: createEmptyObject
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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);
}
示例12: putObject
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的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));
}
示例13: populateMetadataWithEncryptionParams
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
private void populateMetadataWithEncryptionParams(ObjectMetadata source, ObjectMetadata destination) {
Map<String, String> userMetadataSource = source.getUserMetadata();
Map<String, String> userMetadataDestination = destination.getUserMetadata();
String[] headersToCopy = { Headers.CRYPTO_CEK_ALGORITHM,
Headers.CRYPTO_IV, Headers.CRYPTO_KEY, Headers.CRYPTO_KEY_V2,
Headers.CRYPTO_KEYWRAP_ALGORITHM, Headers.CRYPTO_TAG_LENGTH,
Headers.MATERIALS_DESCRIPTION,
Headers.UNENCRYPTED_CONTENT_LENGTH,
Headers.UNENCRYPTED_CONTENT_MD5 };
if (userMetadataSource != null) {
if(userMetadataDestination == null){
userMetadataDestination= new HashMap<String,String>();
destination.setUserMetadata(userMetadataDestination);
}
String headerValue;
for(String header : headersToCopy){
headerValue = userMetadataSource.get(header);
if(headerValue != null){
userMetadataDestination.put(header, headerValue);
}
}
}
}
示例14: put
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
public void put(InputStream inputStream, Long contentLength, URI destination) {
checkRequiredJigsawModuleIsOnPath();
try {
S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
String bucketName = s3RegionalResource.getBucketName();
String s3BucketKey = s3RegionalResource.getKey();
configureClient(s3RegionalResource);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream, objectMetadata);
LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
amazonS3Client.putObject(putObjectRequest);
} catch (AmazonClientException e) {
throw ResourceExceptions.putFailed(destination, e);
}
}
示例15: storeFileInBucket
import com.amazonaws.services.s3.model.ObjectMetadata; //導入依賴的package包/類
public static void storeFileInBucket(String filename, InputStream is, ObjectMetadata metadata) {
String bucketName = properties.getProperty(S3_BUCKET_NAME);
try {
LOG.info("Storing file {} in S3 bucket {}.", filename, bucketName);
S3.putObject(bucketName, filename, is, metadata);
S3.setObjectAcl(bucketName, filename, CannedAccessControlList.PublicRead);
} catch (AmazonServiceException e) {
LOG.error("Exception occured when fetching file from S3 bucket {}.", bucketName, e);
}
}