本文整理匯總了Java中com.amazonaws.services.s3.model.PutObjectRequest.setStorageClass方法的典型用法代碼示例。如果您正苦於以下問題:Java PutObjectRequest.setStorageClass方法的具體用法?Java PutObjectRequest.setStorageClass怎麽用?Java PutObjectRequest.setStorageClass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.amazonaws.services.s3.model.PutObjectRequest
的用法示例。
在下文中一共展示了PutObjectRequest.setStorageClass方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: uploadTextToS3Bucket
import com.amazonaws.services.s3.model.PutObjectRequest; //導入方法依賴的package包/類
public void uploadTextToS3Bucket(String bucketName, String key, String content, String storageClass) throws TranscodeException{
ObjectMetadata objectMetaData = new ObjectMetadata();
byte[] bytes = content.getBytes();
objectMetaData.setContentLength(bytes.length);
objectMetaData.setContentType("text/html; charset=utf-8");
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetaData);
if (storageClass.equalsIgnoreCase(StorageClass.ReducedRedundancy.name())) {
putObjectRequest.setStorageClass(StorageClass.ReducedRedundancy);
}
final long fileSizeInBytes = bytes.length;
putObjectRequest.setGeneralProgressListener(new ProgressListener() {
private long bytesTransferred = 0;
private int currentPercentage = 0;
@Override
public void progressChanged(ProgressEvent progressEvent) {
bytesTransferred += progressEvent.getBytesTransferred();
int percentTransferred = (int) (bytesTransferred * 100 / fileSizeInBytes);
if (percentTransferred%10 == 0 && percentTransferred != currentPercentage) {
logger.info("Transferred {}% of {} bytes.", percentTransferred, fileSizeInBytes);
currentPercentage = percentTransferred;
}
}
});
Upload upload = tm.upload(putObjectRequest);
if (upload!=null) {
upload.waitForCompletion();
} else {
logger.error("Did not get upload detail from S3 for asset with key " + key);
throw new TranscodeException("Did not get upload detail from S3 for asset with key " + key);
}
} catch (AmazonClientException | InterruptedException e) {
throw new TranscodeException(e.getMessage());
}
}
示例2: writeData
import com.amazonaws.services.s3.model.PutObjectRequest; //導入方法依賴的package包/類
private void writeData( AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata, StorageClass storage, String mimetype, cfData data, int retry, int retryseconds, String acl, String aes256key, Map<String, String> customheaders ) throws Exception {
if ( mimetype == null ) {
if ( data.getDataType() == cfData.CFBINARYDATA )
mimetype = "application/unknown";
else if ( cfData.isSimpleValue( data ) )
mimetype = "text/plain";
else
mimetype = "application/json";
// Check to see if the mime type is in the metadata
if ( metadata != null && metadata.containsKey( "Content-Type" ) )
mimetype = metadata.get( "Content-Type" );
}
InputStream ios = null;
long size = 0;
if ( data.getDataType() == cfData.CFSTRINGDATA ) {
ios = new java.io.ByteArrayInputStream( data.getString().getBytes() );
size = data.getString().length();
} else if ( data.getDataType() == cfData.CFBINARYDATA ) {
ios = new java.io.ByteArrayInputStream( ( (cfBinaryData) data ).getByteArray() );
size = ( (cfBinaryData) data ).getLength();
} else {
serializejson json = new serializejson();
StringBuilder out = new StringBuilder();
json.encodeJSON( out, data, false, CaseType.MAINTAIN, DateType.LONG );
size = out.length();
mimetype = "application/json";
ios = new java.io.ByteArrayInputStream( out.toString().getBytes() );
}
// Setup the object data
ObjectMetadata omd = new ObjectMetadata();
if ( metadata != null )
omd.setUserMetadata( metadata );
omd.setContentType( mimetype );
omd.setContentLength( size );
AmazonS3 s3Client = getAmazonS3( amazonKey );
// Let us run around the number of attempts
int attempts = 0;
while ( attempts < retry ) {
try {
PutObjectRequest por = new PutObjectRequest( bucket, key, ios, omd );
por.setStorageClass( storage );
if ( aes256key != null && !aes256key.isEmpty() )
por.setSSECustomerKey( new SSECustomerKey( aes256key ) );
if ( acl != null && !acl.isEmpty() )
por.setCannedAcl( amazonKey.getAmazonCannedAcl( acl ) );
if ( customheaders != null && !customheaders.isEmpty() ) {
Iterator<String> it = customheaders.keySet().iterator();
while ( it.hasNext() ) {
String k = it.next();
por.putCustomRequestHeader( k, customheaders.get( k ) );
}
}
s3Client.putObject( por );
break;
} catch ( Exception e ) {
cfEngine.log( "Failed: AmazonS3Write(bucket=" + bucket + "; key=" + key + "; attempt=" + ( attempts + 1 ) + "; exception=" + e.getMessage() + ")" );
attempts++;
if ( attempts == retry )
throw e;
else
Thread.sleep( retryseconds * 1000 );
}
}
}
示例3: writeFile
import com.amazonaws.services.s3.model.PutObjectRequest; //導入方法依賴的package包/類
private void writeFile( AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata, StorageClass storage, String localpath, int retry, int retryseconds, boolean deletefile, boolean background, String callback, String callbackdata, String appname, String acl, String aes256key, Map<String, String> customheaders ) throws Exception {
File localFile = new File( localpath );
if ( !localFile.isFile() )
throw new Exception( "The file specified does not exist: " + localpath );
// Push this to the background loader to handle and return immediately
if ( background ) {
BackgroundUploader.acceptFile( amazonKey, bucket, key, metadata, storage, localpath, retry, retryseconds, deletefile, callback, callbackdata, appname, acl, aes256key, customheaders );
return;
}
// Setup the object data
ObjectMetadata omd = new ObjectMetadata();
if ( metadata != null )
omd.setUserMetadata( metadata );
AmazonS3 s3Client = getAmazonS3( amazonKey );
// Let us run around the number of attempts
int attempts = 0;
while ( attempts < retry ) {
try {
PutObjectRequest por = new PutObjectRequest( bucket, key, localFile );
por.setMetadata( omd );
por.setStorageClass( storage );
if ( acl != null && !acl.isEmpty() )
por.setCannedAcl( amazonKey.getAmazonCannedAcl( acl ) );
if ( aes256key != null && !aes256key.isEmpty() )
por.setSSECustomerKey( new SSECustomerKey( aes256key ) );
if ( customheaders != null && !customheaders.isEmpty() ) {
Iterator<String> it = customheaders.keySet().iterator();
while ( it.hasNext() ) {
String k = it.next();
por.putCustomRequestHeader( k, customheaders.get( k ) );
}
}
s3Client.putObject( por );
break;
} catch ( Exception e ) {
cfEngine.log( "Failed: AmazonS3Write(bucket=" + bucket + "key=" + key + "; file=" + localFile + "; attempt=" + ( attempts + 1 ) + "; exception=" + e.getMessage() + ")" );
attempts++;
if ( attempts == retry )
throw e;
else
Thread.sleep( retryseconds * 1000 );
}
}
// delete the file now that it is a success
if ( deletefile )
localFile.delete();
}