-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathS3URLConnection.java
217 lines (181 loc) · 6.5 KB
/
S3URLConnection.java
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package au.org.ala.images;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Pattern;
/**
* Support for non standard s3:// URLs, the URL should be in the form
* s3://accesskey:secretKey@bucketname/path
*
* The accesskey and secretKey are optional, if not provided the default credentials will be used.
*/
public class S3URLConnection extends URLConnection {
private static final Logger log = LoggerFactory.getLogger(S3URLConnection.class);
private S3Object object;
private String endpoint;
private boolean pathStyleAccessEnabled = false;
/**
* Constructs a URL connection to the specified URL. A connection to
* the object referenced by the URL is not created.
*
* @param url the specified URL.
*/
protected S3URLConnection(URL url) {
super(url);
if (!url.getProtocol().equals("s3")) {
throw new IllegalArgumentException("URL must use s3 protocol");
}
if (Strings.isBlank(url.getHost())) {
throw new IllegalArgumentException("URL must have a bucket name");
}
// TODO support path based.
//https://bucket-name.s3.region-code.amazonaws.com/key-name
var host = url.getHost();
if (!host.matches("(.*)\\.s3\\.(.*\\.)?amazonaws\\.com")) {
throw new IllegalArgumentException("URL host must be in the form bucketname.s3.region-code.amazonaws.com");
}
if (Strings.isBlank(url.getPath())) {
throw new IllegalArgumentException("URL must have a key");
}
if (url.getUserInfo() != null && url.getUserInfo().split(":").length != 2) {
throw new IllegalArgumentException("URL user info must be in the form accesskey:secretKey");
}
}
@Override
public void connect() throws IOException {
AmazonS3 client;
URI uri;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
throw new IOException(e);
}
var host = uri.getHost();
var pattern = Pattern.compile("(.*)\\.s3\\.(.*\\.)?amazonaws\\.com");
var matcher = pattern.matcher(host);
String bucket = Strings.EMPTY;
String region = Strings.EMPTY;
if (matcher.find()) {
var groupCount = matcher.groupCount();
bucket = matcher.group(1);
region = matcher.group(2);
}
if (!host.matches("(.*)\\.s3\\.(.*\\.)?amazonaws\\.com")) {
throw new IllegalArgumentException("URL host must be in the form bucketname.s3.region-code.amazonaws.com");
}
var builder = AmazonS3ClientBuilder.standard();
if (Strings.isNotBlank(uri.getUserInfo())) {
var parts = uri.getUserInfo().split(":");
builder = builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(parts[0], parts[1])));
}
if (Strings.isNotBlank(region)) {
if (Strings.isNotBlank(endpoint)) {
builder = builder.withEndpointConfiguration(new AmazonS3ClientBuilder.EndpointConfiguration(endpoint, region));
} else {
builder = builder.withRegion(region);
}
}
if (pathStyleAccessEnabled) {
builder = builder.withPathStyleAccessEnabled(true);
}
client = builder.build();
var key = uri.getPath();
if (key.startsWith("/")) {
key = key.substring(1);
}
log.trace("Connecting to s3 bucket: {} key: {}", bucket, key);
object = client.getObject(bucket, key);
// if (url.host.matches('s3\\.(.*\\.)?amazonaws\\.com')) {
// bucketname = url.path.substring(1, url.path.indexOf('/', 1))
// key = url.path.substring(url.path.indexOf('/', 1) + 1)
// } else {
// bucketname = url.host.substring(0, url.host.indexOf('.'))
// key = url.path.substring(1)
// }
}
public void disconnect() {
if (object != null) {
try {
object.close();
} catch (IOException e) { /* ignored */ }
object = null;
}
}
private void ensureConnected() {
if (object == null) {
try {
connect();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public String getContentEncoding() {
ensureConnected();
return object.getObjectMetadata().getContentEncoding();
}
@Override
public long getContentLengthLong() {
ensureConnected();
return object.getObjectMetadata().getContentLength();
}
@Override
public String getContentType() {
ensureConnected();
return object.getObjectMetadata().getContentType();
}
@Override
public long getExpiration() {
ensureConnected();
return object.getObjectMetadata().getExpirationTime().getTime();
}
@Override
public long getLastModified() {
ensureConnected();
return object.getObjectMetadata().getLastModified().getTime();
}
@Override
public InputStream getInputStream() throws IOException {
ensureConnected();
return object.getObjectContent();
}
@Override
public String getHeaderField(String name) {
ensureConnected();
return object.getObjectMetadata().getRawMetadataValue(name).toString();
}
/**
* For testing, allows overriding the default amazon s3 endpoint
*
* @return the endpoint
*/
public String getEndpoint() {
return endpoint;
}
/**
* For testing, allows overriding the default amazon s3 endpoint
*
* @param endpoint the new endpoint to use, eg localstack.local
*/
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public boolean isPathStyleAccessEnabled() {
return pathStyleAccessEnabled;
}
public void setPathStyleAccessEnabled(boolean pathStyleAccessEnabled) {
this.pathStyleAccessEnabled = pathStyleAccessEnabled;
}
}