use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project xin-open-enterprise-platform by hui0xin.
the class S3FileUploadService method createObjectMetadata.
/**
* @param uploadMetadata
* @return
*/
private ObjectMetadata createObjectMetadata(final FileUploadMetadata uploadMetadata) {
if (uploadMetadata == null) {
return null;
}
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(uploadMetadata.getContentLength());
objectMetadata.setContentType(uploadMetadata.getContentType());
objectMetadata.setCacheControl("max-age=" + this.properties.getCacheControlMaxAge());
return objectMetadata;
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project xin-open-enterprise-platform by hui0xin.
the class S3FileUploadService method uploadFile.
@Override
public FileUploadVo uploadFile(final String fileName, final File file, final FileUploadMetadata uploadMetadata) {
final String key = this.getKey(fileName);
final ObjectMetadata objectMetadata = this.createObjectMetadata(uploadMetadata);
this.s3Client.putObject(new PutObjectRequest(this.properties.getBucketName(), key, file).withMetadata(objectMetadata));
return FileUploadVo.builder().fileName(fileName).key(key).url(this.getSignedUrl(fileName)).build();
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project pipes by pipecraft.
the class S3Bucket method getObjectMetadata.
@Override
public S3ObjectSummary getObjectMetadata(String key) throws IOException {
if (isFolderPath(key)) {
throw new FileNotFoundException("File not found: '" + getBucketName() + "/" + key + "'");
}
try {
ObjectMetadata meta = s3.getObjectMetadata(getBucketName(), key);
S3ObjectSummary res = new S3ObjectSummary();
res.setBucketName(getBucketName());
res.setKey(key);
res.setETag(meta.getETag());
res.setLastModified(meta.getLastModified());
res.setSize(meta.getContentLength());
res.setStorageClass(meta.getStorageClass());
return res;
} catch (AmazonClientException ace) {
throw mapToIOException(extractAmazonServiceException(ace), "Failed getting metadata of '" + getBucketName() + "/" + key + "'");
}
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.
the class S3 method setMetaData.
public void setMetaData(String bucketName, String objectName, Struct metadata) throws PageException, S3Exception {
Decision dec = CFMLEngineFactory.getInstance().getDecisionUtil();
Cast cas = CFMLEngineFactory.getInstance().getCastUtil();
Iterator<Entry<Key, Object>> it = metadata.entryIterator();
Entry<Key, Object> e;
ObjectMetadata metadataCopy = new ObjectMetadata();
Map<String, String> data = new ConcurrentHashMap<String, String>();
while (it.hasNext()) {
e = it.next();
metadataCopy.addUserMetadata(toMetaDataKey(e.getKey()), cas.toString(e.getValue()));
}
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName);
if (!Util.isEmpty(objectName)) {
AmazonS3Client client = getAmazonS3(bucketName, null);
try {
S3BucketWrapper bw = get(bucketName);
if (bw == null)
throw new S3Exception("there is no bucket [" + bucketName + "]");
Bucket b = bw.getBucket();
CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName).withNewObjectMetadata(metadataCopy);
client.copyObject(request);
flushExists(bucketName, objectName);
} finally {
client.release();
}
} else
// TOOD possible?
throw new S3Exception("cannot set metadata for a bucket");
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.
the class S3 method write.
/**
* @param bucketName target bucket to create if not already exists
* @param objectName object to create
* @param data object content
* @param acl access control list
* @param region if bucket is not already existing, it get created using that region, if it is not
* defined the default region defined with the constructor is used
* @throws S3Exception
*/
public void write(String bucketName, String objectName, String data, String mimeType, Charset charset, Object acl, String region) throws IOException {
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName, false);
flushExists(bucketName, objectName);
AmazonS3Client client = getAmazonS3(bucketName, region);
String ct = toContentType(mimeType, charset, null);
byte[] bytes = charset == null ? data.getBytes() : data.getBytes(charset);
// unlikely this ever happen, so we do not write extra code for this
if (data.length() > maxSize) {
File tmp = File.createTempFile("writeString-", ".txt");
try {
Util.copy(new ByteArrayInputStream(bytes), new FileOutputStream(tmp), true, true);
write(bucketName, objectName, tmp, acl, region);
return;
} finally {
tmp.delete();
}
} else {
ObjectMetadata md = new ObjectMetadata();
if (ct != null)
md.setContentType(ct);
md.setLastModified(new Date());
// create a PutObjectRequest passing the folder name suffixed by /
md.setContentLength(bytes.length);
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes), md);
if (acl != null)
setACL(por, acl);
try {
// send request to S3 to create folder
try {
client.putObject(por);
flushExists(bucketName, objectName);
} catch (AmazonServiceException ase) {
if (ase.getErrorCode().equals("EntityTooLarge")) {
S3Exception s3e = toS3Exception(ase);
if (s3e.getProposedSize() != 0 && s3e.getProposedSize() < maxSize) {
maxSize = s3e.getProposedSize();
write(bucketName, objectName, data, mimeType, charset, acl, region);
return;
}
throw s3e;
}
if (ase.getErrorCode().equals("NoSuchBucket")) {
createDirectory(bucketName, acl, region);
write(bucketName, objectName, data, mimeType, charset, acl, region);
return;
} else
throw toS3Exception(ase);
}
} catch (AmazonServiceException se) {
throw toS3Exception(se);
} finally {
client.release();
}
}
}
Aggregations