本文整理汇总了Java中com.amazonaws.services.s3.model.PutObjectResult类的典型用法代码示例。如果您正苦于以下问题:Java PutObjectResult类的具体用法?Java PutObjectResult怎么用?Java PutObjectResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PutObjectResult类属于com.amazonaws.services.s3.model包,在下文中一共展示了PutObjectResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putObject
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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;
}
示例2: putObject
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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;
}
示例3: putObject
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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));
}
示例4: putObjectUsingMetadata
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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);
}
}
示例5: shouldCopyObject
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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 shouldCopyObject() 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;
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final com.amazonaws.services.s3.model.S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Sourcefile and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
}
示例6: shouldGetObjectMetadata
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的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);
}
示例7: uploadPage
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void uploadPage() throws IOException, TemplateException {
doCallRealMethod().when(uploader).uploadPage(any());
doReturn(mock(PutObjectResult.class)).when(s3client).putObject(any());
URL url = new URL("http://www.example.com");
doReturn(url).when(s3client).getUrl(anyString(), anyString());
String result = uploader.uploadPage("content");
verify(s3client, times(1)).putObject(any());
verify(s3client, times(1)).getUrl(anyString(), anyString());
assertEquals(result, "http://www.example.com");
}
示例8: testPut
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Test
public void testPut() {
ModelBucket bucket = getService(ModelBucket.class);
InputStream stream = new ByteArrayInputStream("file content".getBytes());
ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class);
PutObjectResult expected = new PutObjectResult();
when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected);
assertEquals(expected, bucket.put("path", stream, 12L));
PutObjectRequest request = requestCaptor.getValue();
assertEquals("model-bucket", request.getBucketName());
assertEquals("path", request.getKey());
assertEquals(stream, request.getInputStream());
assertEquals(12L, request.getMetadata().getContentLength());
List<Grant> grants = request.getAccessControlList().getGrantsAsList();
assertEquals(1, grants.size());
assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee());
assertEquals(Permission.Read, grants.get(0).getPermission());
}
示例9: simpleUploadFile
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
public static ObjectMetadata simpleUploadFile(S3Client client, byte[] bytes, String bucket, String key) throws Exception
{
byte[] md5 = md5(bytes, bytes.length);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(bytes.length);
metadata.setLastModified(new Date());
metadata.setContentMD5(S3Utils.toBase64(md5));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, new ByteArrayInputStream(bytes), metadata);
PutObjectResult putObjectResult = client.putObject(putObjectRequest);
if ( !putObjectResult.getETag().equals(S3Utils.toHex(md5)) )
{
throw new Exception("Unable to match MD5 for config");
}
return metadata;
}
示例10: starting
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Override
protected void starting(Description description)
{
super.starting(description);
outputPath = new File(
"target" + Path.SEPARATOR + description.getClassName() + Path.SEPARATOR + description.getMethodName())
.getPath();
Attribute.AttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
attributes.put(DAG.DAGContext.APPLICATION_ID, description.getClassName());
attributes.put(DAG.DAGContext.APPLICATION_PATH, outputPath);
context = mockOperatorContext(1, attributes);
underTest = new S3Reconciler();
underTest.setAccessKey("");
underTest.setSecretKey("");
underTest.setup(context);
MockitoAnnotations.initMocks(this);
PutObjectResult result = new PutObjectResult();
result.setETag(outputPath);
when(s3clientMock.putObject((PutObjectRequest)any())).thenReturn(result);
underTest.setS3client(s3clientMock);
}
示例11: uploadObject
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Override
public PutObjectResult uploadObject(final String bucketName, final String fileName, final InputStream inputStream,
final CannedAccessControlList cannedAcl) throws AmazonClientException, AmazonServiceException, IOException {
LOGGER.info("uploadObject invoked, bucketName: {} , fileName: {}, cannedAccessControlList: {}", bucketName, fileName, cannedAcl);
File tempFile = null;
PutObjectRequest putObjectRequest = null;
PutObjectResult uploadResult = null;
try {
// Create temporary file from stream to avoid 'out of memory' exception
tempFile = AWSUtil.createTempFileFromStream(inputStream);
putObjectRequest = new PutObjectRequest(bucketName, fileName, tempFile).withCannedAcl(cannedAcl);
uploadResult = uploadObject(putObjectRequest);
} finally {
AWSUtil.deleteTempFile(tempFile); // Delete the temporary file once uploaded
}
return uploadResult;
}
示例12: createDirectory
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Override
public PutObjectResult createDirectory(final String bucketName, final String dirName,
final boolean isPublicAccessible) throws AmazonClientException, AmazonServiceException {
LOGGER.info("createDirectory invoked, bucketName: {}, dirName: {} and isPublicAccessible: {}", bucketName, dirName, isPublicAccessible);
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// Create empty content,since creating empty folder needs an empty content
final InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// Create a PutObjectRequest passing the directory name suffixed by '/'
final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dirName + AWSUtilConstants.SEPARATOR,
emptyContent, metadata);
if(isPublicAccessible){
putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
}
return s3client.putObject(putObjectRequest);
}
示例13: testConditionalGet
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Test
public void testConditionalGet() throws Exception {
assumeTrue(!blobStoreType.equals("b2"));
String blobName = "blob-name";
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(BYTE_SOURCE.size());
PutObjectResult result = client.putObject(containerName, blobName,
BYTE_SOURCE.openStream(), metadata);
S3Object object = client.getObject(
new GetObjectRequest(containerName, blobName)
.withMatchingETagConstraint(result.getETag()));
try (InputStream is = object.getObjectContent()) {
assertThat(is).isNotNull();
ByteStreams.copy(is, ByteStreams.nullOutputStream());
}
object = client.getObject(
new GetObjectRequest(containerName, blobName)
.withNonmatchingETagConstraint(result.getETag()));
assertThat(object).isNull();
}
示例14: uploadEntity
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
@Override
public boolean uploadEntity(String bucketName, String keyName, File file) {
LOG.info("Uploads the specified file " + file
+ " to Amazon S3 under the specified bucket " + bucketName
+ " and key name " + keyName);
try {
PutObjectResult putObjectResult = amazonS3Client.putObject(bucketName, keyName, file);
if (putObjectResult != null) {
return true;
}
} catch (AmazonServiceException ase) {
LOG.error(ase.getMessage(), ase);
} catch (AmazonClientException ace) {
LOG.error(ace.getMessage(), ace);
}
return false;
}
示例15: store
import com.amazonaws.services.s3.model.PutObjectResult; //导入依赖的package包/类
protected MediaMetadata store(String bucketName, String key,String fileName,InputStream data, MediaType contentType, Date lastModified)
throws IOException
{
File tmpFile = cache(data);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentType.toString());
metadata.setContentLength(tmpFile.length());
metadata.setLastModified(lastModified==null ? new Date() : lastModified);
if (lastModified!=null) {
metadata.addUserMetadata("last-modified", DateUtils.format(lastModified,DateUtils.FORMAT_RFC_3339.get(0)));
}
InputStream is = new FileInputStream(tmpFile);
try {
PutObjectResult result = s3Client.putObject(bucketName, key, is, metadata);
is.close();
return new S3Metadata(fileName,contentType,lastModified,metadata.getContentLength());
} catch (AmazonClientException ex) {
is.close();
throw new IOException("Cannot create object due to S3 error.",ex);
} finally {
tmpFile.delete();
}
}