Skip to content

Commit b5f05f6

Browse files
authored
Remove BlobContainer.move() method (#31100)
closes #30680
1 parent 749d390 commit b5f05f6

File tree

22 files changed

+55
-447
lines changed

22 files changed

+55
-447
lines changed

modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobContainer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws
8282
throw new UnsupportedOperationException("URL repository doesn't support this operation");
8383
}
8484

85-
@Override
86-
public void move(String from, String to) throws IOException {
87-
throw new UnsupportedOperationException("URL repository doesn't support this operation");
88-
}
89-
9085
/**
9186
* This operation is not supported by URLBlobContainer
9287
*/

plugins/repository-azure/qa/microsoft-azure-storage/src/test/java/org/elasticsearch/repositories/azure/AzureStorageTestServer.java

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.concurrent.atomic.AtomicLong;
3434

3535
import static java.nio.charset.StandardCharsets.UTF_8;
36-
import static java.util.Collections.emptyList;
3736
import static java.util.Collections.emptyMap;
3837
import static java.util.Collections.singletonMap;
3938

@@ -159,44 +158,13 @@ private static PathTrie<RequestHandler> defaultHandlers(final String endpoint, f
159158
objectsPaths("PUT " + endpoint + "/{container}").forEach(path ->
160159
handlers.insert(path, (params, headers, body, requestId) -> {
161160
final String destContainerName = params.get("container");
161+
final String destBlobName = objectName(params);
162162

163163
final Container destContainer =containers.get(destContainerName);
164164
if (destContainer == null) {
165165
return newContainerNotFoundError(requestId);
166166
}
167-
168-
final String destBlobName = objectName(params);
169-
170-
// Request is a copy request
171-
List<String> headerCopySource = headers.getOrDefault("x-ms-copy-source", emptyList());
172-
if (headerCopySource.isEmpty() == false) {
173-
String srcBlobName = headerCopySource.get(0);
174-
175-
Container srcContainer = null;
176-
for (Container container : containers.values()) {
177-
String prefix = endpoint + "/" + container.name + "/";
178-
if (srcBlobName.startsWith(prefix)) {
179-
srcBlobName = srcBlobName.replaceFirst(prefix, "");
180-
srcContainer = container;
181-
break;
182-
}
183-
}
184-
185-
if (srcContainer == null || srcContainer.objects.containsKey(srcBlobName) == false) {
186-
return newBlobNotFoundError(requestId);
187-
}
188-
189-
byte[] bytes = srcContainer.objects.get(srcBlobName);
190-
if (bytes != null) {
191-
destContainer.objects.put(destBlobName, bytes);
192-
return new Response(RestStatus.ACCEPTED, singletonMap("x-ms-copy-status", "success"), "text/plain", EMPTY_BYTE);
193-
} else {
194-
return newBlobNotFoundError(requestId);
195-
}
196-
} else {
197-
destContainer.objects.put(destBlobName, body);
198-
}
199-
167+
destContainer.objects.put(destBlobName, body);
200168
return new Response(RestStatus.CREATED, emptyMap(), "text/plain", EMPTY_BYTE);
201169
})
202170
);

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,6 @@ public Map<String, BlobMetaData> listBlobsByPrefix(@Nullable String prefix) thro
127127
}
128128
}
129129

130-
@Override
131-
public void move(String sourceBlobName, String targetBlobName) throws IOException {
132-
logger.trace("move({}, {})", sourceBlobName, targetBlobName);
133-
try {
134-
String source = keyPath + sourceBlobName;
135-
String target = keyPath + targetBlobName;
136-
137-
logger.debug("moving blob [{}] to [{}] in container {{}}", source, target, blobStore);
138-
139-
blobStore.moveBlob(source, target);
140-
} catch (URISyntaxException | StorageException e) {
141-
logger.warn("can not move blob [{}] to [{}] in container {{}}: {}", sourceBlobName, targetBlobName, blobStore, e.getMessage());
142-
throw new IOException(e);
143-
}
144-
}
145-
146130
@Override
147131
public Map<String, BlobMetaData> listBlobs() throws IOException {
148132
logger.trace("listBlobs()");

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,23 @@ public boolean doesContainerExist()
9797
return this.client.doesContainerExist(this.clientName, this.locMode, container);
9898
}
9999

100-
public boolean blobExists(String blob) throws URISyntaxException, StorageException
101-
{
100+
public boolean blobExists(String blob) throws URISyntaxException, StorageException {
102101
return this.client.blobExists(this.clientName, this.locMode, container, blob);
103102
}
104103

105-
public void deleteBlob(String blob) throws URISyntaxException, StorageException
106-
{
104+
public void deleteBlob(String blob) throws URISyntaxException, StorageException {
107105
this.client.deleteBlob(this.clientName, this.locMode, container, blob);
108106
}
109107

110-
public InputStream getInputStream(String blob) throws URISyntaxException, StorageException, IOException
111-
{
108+
public InputStream getInputStream(String blob) throws URISyntaxException, StorageException, IOException {
112109
return this.client.getInputStream(this.clientName, this.locMode, container, blob);
113110
}
114111

115-
public Map<String,BlobMetaData> listBlobsByPrefix(String keyPath, String prefix)
112+
public Map<String, BlobMetaData> listBlobsByPrefix(String keyPath, String prefix)
116113
throws URISyntaxException, StorageException {
117114
return this.client.listBlobsByPrefix(this.clientName, this.locMode, container, keyPath, prefix);
118115
}
119116

120-
public void moveBlob(String sourceBlob, String targetBlob) throws URISyntaxException, StorageException
121-
{
122-
this.client.moveBlob(this.clientName, this.locMode, container, sourceBlob, targetBlob);
123-
}
124-
125117
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws URISyntaxException, StorageException {
126118
this.client.writeBlob(this.clientName, this.locMode, container, blobName, inputStream, blobSize);
127119
}

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ InputStream getInputStream(String account, LocationMode mode, String container,
5757
Map<String,BlobMetaData> listBlobsByPrefix(String account, LocationMode mode, String container, String keyPath, String prefix)
5858
throws URISyntaxException, StorageException;
5959

60-
void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob)
61-
throws URISyntaxException, StorageException;
62-
6360
void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize) throws
6461
URISyntaxException, StorageException;
6562

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageServiceImpl.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,24 +287,6 @@ enumBlobListingDetails, null, generateOperationContext(account))) {
287287
return blobsBuilder.immutableMap();
288288
}
289289

290-
@Override
291-
public void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob)
292-
throws URISyntaxException, StorageException {
293-
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}]", container, sourceBlob, targetBlob);
294-
295-
CloudBlobClient client = this.getSelectedClient(account, mode);
296-
CloudBlobContainer blobContainer = client.getContainerReference(container);
297-
CloudBlockBlob blobSource = blobContainer.getBlockBlobReference(sourceBlob);
298-
if (SocketAccess.doPrivilegedException(() -> blobSource.exists(null, null, generateOperationContext(account)))) {
299-
CloudBlockBlob blobTarget = blobContainer.getBlockBlobReference(targetBlob);
300-
SocketAccess.doPrivilegedVoidException(() -> {
301-
blobTarget.startCopy(blobSource, null, null, null, generateOperationContext(account));
302-
blobSource.delete(DeleteSnapshotsOption.NONE, null, null, generateOperationContext(account));
303-
});
304-
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done", container, sourceBlob, targetBlob);
305-
}
306-
}
307-
308290
@Override
309291
public void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize)
310292
throws URISyntaxException, StorageException {

plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceMock.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,6 @@ public Map<String, BlobMetaData> listBlobsByPrefix(String account, LocationMode
106106
return blobsBuilder.immutableMap();
107107
}
108108

109-
@Override
110-
public void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob)
111-
throws URISyntaxException, StorageException {
112-
for (String blobName : blobs.keySet()) {
113-
if (endsWithIgnoreCase(blobName, sourceBlob)) {
114-
ByteArrayOutputStream outputStream = blobs.get(blobName);
115-
blobs.put(blobName.replace(sourceBlob, targetBlob), outputStream);
116-
blobs.remove(blobName);
117-
}
118-
}
119-
}
120-
121109
@Override
122110
public void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize)
123111
throws URISyntaxException, StorageException {
@@ -137,7 +125,7 @@ public void writeBlob(String account, LocationMode mode, String container, Strin
137125
* @param prefix the prefix to look for
138126
* @see java.lang.String#startsWith
139127
*/
140-
public static boolean startsWithIgnoreCase(String str, String prefix) {
128+
private static boolean startsWithIgnoreCase(String str, String prefix) {
141129
if (str == null || prefix == null) {
142130
return false;
143131
}
@@ -152,29 +140,6 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
152140
return lcStr.equals(lcPrefix);
153141
}
154142

155-
/**
156-
* Test if the given String ends with the specified suffix,
157-
* ignoring upper/lower case.
158-
*
159-
* @param str the String to check
160-
* @param suffix the suffix to look for
161-
* @see java.lang.String#startsWith
162-
*/
163-
public static boolean endsWithIgnoreCase(String str, String suffix) {
164-
if (str == null || suffix == null) {
165-
return false;
166-
}
167-
if (str.endsWith(suffix)) {
168-
return true;
169-
}
170-
if (str.length() < suffix.length()) {
171-
return false;
172-
}
173-
String lcStr = str.substring(0, suffix.length()).toLowerCase(Locale.ROOT);
174-
String lcPrefix = suffix.toLowerCase(Locale.ROOT);
175-
return lcStr.equals(lcPrefix);
176-
}
177-
178143
private static class PermissionRequiringInputStream extends ByteArrayInputStream {
179144

180145
private PermissionRequiringInputStream(byte[] buf) {

plugins/repository-gcs/qa/google-cloud-storage/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageTestServer.java

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -367,47 +367,6 @@ private static PathTrie<RequestHandler> defaultHandlers(final String endpoint, f
367367
return newResponse(RestStatus.OK, emptyMap(), buildObjectResource(bucket.name, objectId, body));
368368
});
369369

370-
// Rewrite or Copy Object
371-
//
372-
// https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite
373-
// https://cloud.google.com/storage/docs/json_api/v1/objects/copy
374-
handlers.insert("POST " + endpoint + "/storage/v1/b/{srcBucket}/o/{src}/{action}/b/{destBucket}/o/{dest}",
375-
(params, headers, body) -> {
376-
final String action = params.get("action");
377-
if ((action.equals("rewriteTo") == false) && (action.equals("copyTo") == false)) {
378-
return newError(RestStatus.INTERNAL_SERVER_ERROR, "Action not implemented. None of \"rewriteTo\" or \"copyTo\".");
379-
}
380-
final String source = params.get("src");
381-
if (Strings.hasText(source) == false) {
382-
return newError(RestStatus.INTERNAL_SERVER_ERROR, "source object name is missing");
383-
}
384-
final Bucket srcBucket = buckets.get(params.get("srcBucket"));
385-
if (srcBucket == null) {
386-
return newError(RestStatus.NOT_FOUND, "source bucket not found");
387-
}
388-
final String dest = params.get("dest");
389-
if (Strings.hasText(dest) == false) {
390-
return newError(RestStatus.INTERNAL_SERVER_ERROR, "destination object name is missing");
391-
}
392-
final Bucket destBucket = buckets.get(params.get("destBucket"));
393-
if (destBucket == null) {
394-
return newError(RestStatus.NOT_FOUND, "destination bucket not found");
395-
}
396-
final byte[] sourceBytes = srcBucket.objects.get(source);
397-
if (sourceBytes == null) {
398-
return newError(RestStatus.NOT_FOUND, "source object not found");
399-
}
400-
destBucket.objects.put(dest, sourceBytes);
401-
if (action.equals("rewriteTo")) {
402-
final XContentBuilder respBuilder = jsonBuilder();
403-
buildRewriteResponse(respBuilder, destBucket.name, dest, sourceBytes.length);
404-
return newResponse(RestStatus.OK, emptyMap(), respBuilder);
405-
} else {
406-
assert action.equals("copyTo");
407-
return newResponse(RestStatus.OK, emptyMap(), buildObjectResource(destBucket.name, dest, sourceBytes));
408-
}
409-
});
410-
411370
// List Objects
412371
//
413372
// https://cloud.google.com/storage/docs/json_api/v1/objects/list
@@ -701,28 +660,4 @@ private static XContentBuilder buildObjectResource(final XContentBuilder builder
701660
.field("size", String.valueOf(bytes.length))
702661
.endObject();
703662
}
704-
705-
/**
706-
* Builds the rewrite response as defined by
707-
* https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite
708-
*/
709-
private static XContentBuilder buildRewriteResponse(final XContentBuilder builder,
710-
final String destBucket,
711-
final String dest,
712-
final int byteSize) throws IOException {
713-
builder.startObject()
714-
.field("kind", "storage#rewriteResponse")
715-
.field("totalBytesRewritten", String.valueOf(byteSize))
716-
.field("objectSize", String.valueOf(byteSize))
717-
.field("done", true)
718-
.startObject("resource")
719-
.field("kind", "storage#object")
720-
.field("id", String.join("/", destBucket, dest))
721-
.field("name", dest)
722-
.field("bucket", destBucket)
723-
.field("size", String.valueOf(byteSize))
724-
.endObject()
725-
.endObject();
726-
return builder;
727-
}
728663
}

plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainer.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29-
import java.nio.file.FileAlreadyExistsException;
3029
import java.util.Map;
3130

3231
class GoogleCloudStorageBlobContainer extends AbstractBlobContainer {
@@ -74,11 +73,6 @@ public void deleteBlob(String blobName) throws IOException {
7473
blobStore.deleteBlob(buildKey(blobName));
7574
}
7675

77-
@Override
78-
public void move(String sourceBlobName, String targetBlobName) throws IOException {
79-
blobStore.moveBlob(buildKey(sourceBlobName), buildKey(targetBlobName));
80-
}
81-
8276
protected String buildKey(String blobName) {
8377
assert blobName != null;
8478
return path + blobName;

plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.cloud.storage.Bucket;
2828
import com.google.cloud.storage.Storage;
2929
import com.google.cloud.storage.Storage.BlobListOption;
30-
import com.google.cloud.storage.Storage.CopyRequest;
3130
import com.google.cloud.storage.StorageException;
3231
import org.elasticsearch.common.SuppressForbidden;
3332
import org.elasticsearch.common.blobstore.BlobContainer;
@@ -314,29 +313,6 @@ void deleteBlobs(Collection<String> blobNames) throws IOException {
314313
}
315314
}
316315

317-
/**
318-
* Moves a blob within the same bucket
319-
*
320-
* @param sourceBlobName name of the blob to move
321-
* @param targetBlobName new name of the blob in the same bucket
322-
*/
323-
void moveBlob(String sourceBlobName, String targetBlobName) throws IOException {
324-
final BlobId sourceBlobId = BlobId.of(bucket, sourceBlobName);
325-
final BlobId targetBlobId = BlobId.of(bucket, targetBlobName);
326-
final CopyRequest request = CopyRequest.newBuilder()
327-
.setSource(sourceBlobId)
328-
.setTarget(targetBlobId)
329-
.build();
330-
SocketAccess.doPrivilegedVoidIOException(() -> {
331-
// There's no atomic "move" in GCS so we need to copy and delete
332-
storage.copy(request).getResult();
333-
final boolean deleted = storage.delete(sourceBlobId);
334-
if (deleted == false) {
335-
throw new IOException("Failed to move source [" + sourceBlobName + "] to target [" + targetBlobName + "]");
336-
}
337-
});
338-
}
339-
340316
private static String buildKey(String keyPath, String s) {
341317
assert s != null;
342318
return keyPath + s;

plugins/repository-gcs/src/test/java/com/google/cloud/storage/StorageRpcOptionUtils.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import com.google.cloud.storage.spi.v1.StorageRpc;
2323

24-
import static org.mockito.Mockito.mock;
25-
2624
/**
2725
* Utility class that exposed Google SDK package protected methods to
2826
* create specific StorageRpc objects in unit tests.
@@ -42,13 +40,4 @@ public static String getPrefix(final Storage.BlobListOption... options) {
4240
}
4341
return null;
4442
}
45-
46-
public static CopyWriter createCopyWriter(final Blob result) {
47-
return new CopyWriter(mock(StorageOptions.class), mock(StorageRpc.RewriteResponse.class)) {
48-
@Override
49-
public Blob getResult() {
50-
return result;
51-
}
52-
};
53-
}
5443
}

0 commit comments

Comments
 (0)