use of org.jets3t.service.ServiceException in project hadoop by apache.
the class Jets3tNativeFileSystemStore method storeEmptyFile.
@Override
public void storeEmptyFile(String key) throws IOException {
try {
S3Object object = new S3Object(key);
object.setDataInputStream(new ByteArrayInputStream(new byte[0]));
object.setContentType("binary/octet-stream");
object.setContentLength(0);
object.setServerSideEncryptionAlgorithm(serverSideEncryptionAlgorithm);
s3Service.putObject(bucket, object);
} catch (ServiceException e) {
handleException(e, key);
}
}
use of org.jets3t.service.ServiceException in project mucommander by mucommander.
the class S3Object method delete.
@Override
public void delete() throws IOException {
// Note: DELETE on a non-existing resource is a successful request, so we need this check
if (!exists())
throw new IOException();
try {
// Make sure that the directory is empty, abort if not.
// Note that we must not count the parent directory (this file).
boolean isDirectory = isDirectory();
if (isDirectory && service.listObjectsChunked(bucketName, getObjectKey(true), "/", 2, null, false).getObjects().length > 1)
throw new IOException("Directory not empty");
service.deleteObject(bucketName, getObjectKey(isDirectory));
// Update file attributes locally
atts.setExists(false);
atts.setDirectory(false);
atts.setSize(0);
} catch (ServiceException e) {
throw getIOException(e);
}
}
use of org.jets3t.service.ServiceException in project cyberduck by iterate-ch.
the class S3VersionedObjectListService method list.
@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
final ThreadPool pool = ThreadPoolFactory.get("list", concurrency);
try {
final String prefix = this.createPrefix(directory);
final Path bucket = containerService.getContainer(directory);
final AttributedList<Path> children = new AttributedList<>();
final List<Future<Path>> folders = new ArrayList<>();
String priorLastKey = null;
String priorLastVersionId = null;
long revision = 0L;
String lastKey = null;
boolean hasDirectoryPlaceholder = bucket.isRoot() || containerService.isContainer(directory);
do {
final VersionOrDeleteMarkersChunk chunk = session.getClient().listVersionedObjectsChunked(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), prefix, String.valueOf(Path.DELIMITER), new HostPreferences(session.getHost()).getInteger("s3.listing.chunksize"), priorLastKey, priorLastVersionId, false);
// Amazon S3 returns object versions in the order in which they were stored, with the most recently stored returned first.
for (BaseVersionOrDeleteMarker marker : chunk.getItems()) {
final String key = URIEncoder.decode(marker.getKey());
if (String.valueOf(Path.DELIMITER).equals(PathNormalizer.normalize(key))) {
log.warn(String.format("Skipping prefix %s", key));
continue;
}
if (new SimplePathPredicate(PathNormalizer.compose(bucket, key)).test(directory)) {
// Placeholder object, skip
hasDirectoryPlaceholder = true;
continue;
}
final PathAttributes attr = new PathAttributes();
attr.setVersionId("null".equals(marker.getVersionId()) ? null : marker.getVersionId());
if (!StringUtils.equals(lastKey, key)) {
// Reset revision for next file
revision = 0L;
}
attr.setRevision(++revision);
attr.setDuplicate(marker.isDeleteMarker() && marker.isLatest() || !marker.isLatest());
if (marker.isDeleteMarker()) {
attr.setCustom(Collections.singletonMap(KEY_DELETE_MARKER, String.valueOf(true)));
}
attr.setModificationDate(marker.getLastModified().getTime());
attr.setRegion(bucket.attributes().getRegion());
if (marker instanceof S3Version) {
final S3Version object = (S3Version) marker;
attr.setSize(object.getSize());
if (StringUtils.isNotBlank(object.getEtag())) {
attr.setETag(StringUtils.remove(object.getEtag(), "\""));
// The ETag will only be the MD5 of the object data when the object is stored as plaintext or encrypted
// using SSE-S3. If the object is encrypted using another method (such as SSE-C or SSE-KMS) the ETag is
// not the MD5 of the object data.
attr.setChecksum(Checksum.parse(StringUtils.remove(object.getEtag(), "\"")));
}
if (StringUtils.isNotBlank(object.getStorageClass())) {
attr.setStorageClass(object.getStorageClass());
}
}
final Path f = new Path(directory.isDirectory() ? directory : directory.getParent(), PathNormalizer.name(key), EnumSet.of(Path.Type.file), attr);
if (metadata) {
f.withAttributes(attributes.find(f));
}
children.add(f);
lastKey = key;
}
if (references) {
for (Path f : children) {
if (f.attributes().isDuplicate()) {
final Path latest = children.find(new LatestVersionPathPredicate(f));
if (latest != null) {
// Reference version
final AttributedList<Path> versions = new AttributedList<>(latest.attributes().getVersions());
versions.add(f);
latest.attributes().setVersions(versions);
} else {
log.warn(String.format("No current version found for %s", f));
}
}
}
}
final String[] prefixes = chunk.getCommonPrefixes();
for (String common : prefixes) {
if (String.valueOf(Path.DELIMITER).equals(common)) {
log.warn(String.format("Skipping prefix %s", common));
continue;
}
final String key = PathNormalizer.normalize(URIEncoder.decode(common));
if (new SimplePathPredicate(new Path(bucket, key, EnumSet.of(Path.Type.directory))).test(directory)) {
continue;
}
folders.add(this.submit(pool, bucket, directory, URIEncoder.decode(common)));
}
priorLastKey = null != chunk.getNextKeyMarker() ? URIEncoder.decode(chunk.getNextKeyMarker()) : null;
priorLastVersionId = chunk.getNextVersionIdMarker();
listener.chunk(directory, children);
} while (priorLastKey != null);
for (Future<Path> future : folders) {
try {
children.add(future.get());
} catch (InterruptedException e) {
log.error("Listing versioned objects failed with interrupt failure");
throw new ConnectionCanceledException(e);
} catch (ExecutionException e) {
log.warn(String.format("Listing versioned objects failed with execution failure %s", e.getMessage()));
if (e.getCause() instanceof BackgroundException) {
throw (BackgroundException) e.getCause();
}
throw new BackgroundException(e.getCause());
}
}
listener.chunk(directory, children);
if (!hasDirectoryPlaceholder && children.isEmpty()) {
// Only for AWS
if (S3Session.isAwsHostname(session.getHost().getHostname())) {
if (StringUtils.isEmpty(RequestEntityRestStorageService.findBucketInHostname(session.getHost()))) {
throw new NotfoundException(directory.getAbsolute());
}
} else {
// Handle missing prefix for directory placeholders in Minio
final VersionOrDeleteMarkersChunk chunk = session.getClient().listVersionedObjectsChunked(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), String.format("%s%s", this.createPrefix(directory.getParent()), directory.getName()), String.valueOf(Path.DELIMITER), 1, null, null, false);
if (Arrays.stream(chunk.getCommonPrefixes()).map(URIEncoder::decode).noneMatch(common -> common.equals(prefix))) {
throw new NotfoundException(directory.getAbsolute());
}
}
}
return children;
} catch (ServiceException e) {
throw new S3ExceptionMappingService().map("Listing directory {0} failed", e, directory);
} finally {
// Cancel future tasks
pool.shutdown(false);
}
}
use of org.jets3t.service.ServiceException in project cyberduck by iterate-ch.
the class S3LoggingFeature method setConfiguration.
@Override
public void setConfiguration(final Path file, final LoggingConfiguration configuration) throws BackgroundException {
// Logging target bucket
final Path bucket = containerService.getContainer(file);
try {
final S3BucketLoggingStatus status = new S3BucketLoggingStatus(StringUtils.isNotBlank(configuration.getLoggingTarget()) ? configuration.getLoggingTarget() : bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), null);
if (configuration.isEnabled()) {
status.setLogfilePrefix(new HostPreferences(session.getHost()).getProperty("s3.logging.prefix"));
}
session.getClient().setBucketLoggingStatus(bucket.getName(), status, true);
} catch (ServiceException e) {
throw new S3ExceptionMappingService().map("Failure to write attributes of {0}", e, file);
}
}
use of org.jets3t.service.ServiceException in project cyberduck by iterate-ch.
the class S3MetadataFeature method setMetadata.
@Override
public void setMetadata(final Path file, final TransferStatus status) throws BackgroundException {
if (file.isFile() || file.isPlaceholder()) {
if (log.isDebugEnabled()) {
log.debug(String.format("Write metadata %s for file %s", status, file));
}
try {
final StorageObject target = new StorageObject(containerService.getKey(file));
target.replaceAllMetadata(new HashMap<>(status.getMetadata()));
if (status.getTimestamp() != null) {
target.addMetadata(S3TimestampFeature.METADATA_MODIFICATION_DATE, String.valueOf(status.getTimestamp()));
}
try {
// Apply non standard ACL
target.setAcl(accessControlListFeature.toAcl(accessControlListFeature.getPermission(file)));
} catch (AccessDeniedException | InteroperabilityException e) {
log.warn(String.format("Ignore failure %s", e));
}
final Redundancy storageClassFeature = session.getFeature(Redundancy.class);
if (storageClassFeature != null) {
target.setStorageClass(storageClassFeature.getClass(file));
}
final Encryption encryptionFeature = session.getFeature(Encryption.class);
if (encryptionFeature != null) {
final Encryption.Algorithm encryption = encryptionFeature.getEncryption(file);
target.setServerSideEncryptionAlgorithm(encryption.algorithm);
// Set custom key id stored in KMS
target.setServerSideEncryptionKmsKeyId(encryption.key);
}
final Path bucket = containerService.getContainer(file);
final Map<String, Object> metadata = session.getClient().updateObjectMetadata(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), target);
if (metadata.containsKey("version-id")) {
file.attributes().setVersionId(metadata.get("version-id").toString());
}
} catch (ServiceException e) {
final BackgroundException failure = new S3ExceptionMappingService().map("Failure to write attributes of {0}", e, file);
if (file.isPlaceholder()) {
if (failure instanceof NotfoundException) {
// No placeholder file may exist but we just have a common prefix
return;
}
}
throw failure;
}
}
}
Aggregations