| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.s3; |
| 31 | |
|
| 32 | |
import com.amazonaws.AmazonServiceException; |
| 33 | |
import com.amazonaws.services.s3.AmazonS3; |
| 34 | |
import com.amazonaws.services.s3.model.DeleteObjectRequest; |
| 35 | |
import com.jcabi.aspects.Immutable; |
| 36 | |
import com.jcabi.aspects.Loggable; |
| 37 | |
import com.jcabi.log.Logger; |
| 38 | |
import java.io.IOException; |
| 39 | |
import java.util.Iterator; |
| 40 | |
import lombok.EqualsAndHashCode; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | @Immutable |
| 50 | 0 | @EqualsAndHashCode(of = { "regn", "bkt" }) |
| 51 | |
@Loggable(Loggable.DEBUG) |
| 52 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 53 | 2 | final class AwsBucket implements Bucket { |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private final transient Region regn; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient String bkt; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 5 | AwsBucket(final Region reg, final String name) { |
| 71 | 5 | this.regn = reg; |
| 72 | 5 | this.bkt = name; |
| 73 | 5 | } |
| 74 | |
|
| 75 | |
@Override |
| 76 | |
public String toString() { |
| 77 | 0 | return this.bkt; |
| 78 | |
} |
| 79 | |
|
| 80 | |
@Override |
| 81 | |
public Region region() { |
| 82 | 0 | return this.regn; |
| 83 | |
} |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public String name() { |
| 87 | 0 | return this.bkt; |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public Ocket ocket(final String key) { |
| 92 | 2 | return new AwsOcket(this, key); |
| 93 | |
} |
| 94 | |
|
| 95 | |
@Override |
| 96 | |
public boolean exists() throws IOException { |
| 97 | 6 | final AmazonS3 aws = this.regn.aws(); |
| 98 | |
final boolean result; |
| 99 | |
try { |
| 100 | 3 | result = aws.doesBucketExist(this.bkt); |
| 101 | 1 | } catch (final AmazonServiceException ex) { |
| 102 | 1 | throw new IOException( |
| 103 | |
String.format( |
| 104 | |
"failed to check existence of '%s' bucket", |
| 105 | |
this.bkt |
| 106 | |
), |
| 107 | |
ex |
| 108 | |
); |
| 109 | 2 | } |
| 110 | 2 | Logger.debug(this, "Does bucket '%s' exist? %b", this.bkt, result); |
| 111 | 2 | return result; |
| 112 | |
} |
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public void remove(final String key) throws IOException { |
| 116 | |
try { |
| 117 | 0 | final AmazonS3 aws = this.regn.aws(); |
| 118 | 0 | final long start = System.currentTimeMillis(); |
| 119 | 0 | aws.deleteObject(new DeleteObjectRequest(this.bkt, key)); |
| 120 | 0 | Logger.info( |
| 121 | |
this, |
| 122 | |
"ocket '%s' removed in bucket '%s' in %[ms]s", |
| 123 | |
key, this.bkt, System.currentTimeMillis() - start |
| 124 | |
); |
| 125 | 0 | } catch (final AmazonServiceException ex) { |
| 126 | 0 | throw new IOException( |
| 127 | |
String.format( |
| 128 | |
"failed to remove '%s' bucket", |
| 129 | |
this.bkt |
| 130 | |
), |
| 131 | |
ex |
| 132 | |
); |
| 133 | 0 | } |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public Iterable<String> list(final String pfx) { |
| 138 | 2 | return new Iterable<String>() { |
| 139 | |
@Override |
| 140 | |
public Iterator<String> iterator() { |
| 141 | 1 | return new AwsListIterator( |
| 142 | |
AwsBucket.this.regn, AwsBucket.this.bkt, pfx |
| 143 | |
); |
| 144 | |
} |
| 145 | |
}; |
| 146 | |
} |
| 147 | |
|
| 148 | |
@Override |
| 149 | |
public int compareTo(final Bucket bucket) { |
| 150 | 0 | return this.name().compareTo(bucket.name()); |
| 151 | |
} |
| 152 | |
|
| 153 | |
} |
| 154 | |
|