Skip to content

Commit 6e0a6d1

Browse files
committed
Remove BlobContainer.move() method (#31100)
closes #30680
1 parent f1256fa commit 6e0a6d1

File tree

22 files changed

+56
-447
lines changed

22 files changed

+56
-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/cloud/azure/blobstore/AzureBlobContainer.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +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,
142-
e.getMessage());
143-
throw new IOException(e);
144-
}
145-
}
146-
147130
@Override
148131
public Map<String, BlobMetaData> listBlobs() throws IOException {
149132
logger.trace("listBlobs()");

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

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

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

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

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

116-
public Map<String,BlobMetaData> listBlobsByPrefix(String keyPath, String prefix) throws URISyntaxException, StorageException
117-
{
113+
public Map<String, BlobMetaData> listBlobsByPrefix(String keyPath, String prefix)
114+
throws URISyntaxException, StorageException {
118115
return this.client.listBlobsByPrefix(this.clientName, this.locMode, container, keyPath, prefix);
119116
}
120117

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

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

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

81-
void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob)
82-
throws URISyntaxException, StorageException;
83-
8481
void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize) throws
8582
URISyntaxException, StorageException;
8683

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,23 +325,6 @@ enumBlobListingDetails, null, generateOperationContext(account))) {
325325
return blobsBuilder.immutableMap();
326326
}
327327

328-
@Override
329-
public void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
330-
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}]", container, sourceBlob, targetBlob);
331-
332-
CloudBlobClient client = this.getSelectedClient(account, mode);
333-
CloudBlobContainer blobContainer = client.getContainerReference(container);
334-
CloudBlockBlob blobSource = blobContainer.getBlockBlobReference(sourceBlob);
335-
if (SocketAccess.doPrivilegedException(() -> blobSource.exists(null, null, generateOperationContext(account)))) {
336-
CloudBlockBlob blobTarget = blobContainer.getBlockBlobReference(targetBlob);
337-
SocketAccess.doPrivilegedVoidException(() -> {
338-
blobTarget.startCopy(blobSource, null, null, null, generateOperationContext(account));
339-
blobSource.delete(DeleteSnapshotsOption.NONE, null, null, generateOperationContext(account));
340-
});
341-
logger.debug("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done", container, sourceBlob, targetBlob);
342-
}
343-
}
344-
345328
@Override
346329
public void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize)
347330
throws URISyntaxException, StorageException {

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ public Map<String, BlobMetaData> listBlobsByPrefix(String account, LocationMode
104104
return blobsBuilder.immutableMap();
105105
}
106106

107-
@Override
108-
public void moveBlob(String account, LocationMode mode, String container, String sourceBlob, String targetBlob) throws URISyntaxException, StorageException {
109-
for (String blobName : blobs.keySet()) {
110-
if (endsWithIgnoreCase(blobName, sourceBlob)) {
111-
ByteArrayOutputStream outputStream = blobs.get(blobName);
112-
blobs.put(blobName.replace(sourceBlob, targetBlob), outputStream);
113-
blobs.remove(blobName);
114-
}
115-
}
116-
}
117-
118107
@Override
119108
public void writeBlob(String account, LocationMode mode, String container, String blobName, InputStream inputStream, long blobSize)
120109
throws URISyntaxException, StorageException {
@@ -134,7 +123,7 @@ public void writeBlob(String account, LocationMode mode, String container, Strin
134123
* @param prefix the prefix to look for
135124
* @see java.lang.String#startsWith
136125
*/
137-
public static boolean startsWithIgnoreCase(String str, String prefix) {
126+
private static boolean startsWithIgnoreCase(String str, String prefix) {
138127
if (str == null || prefix == null) {
139128
return false;
140129
}
@@ -149,29 +138,6 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
149138
return lcStr.equals(lcPrefix);
150139
}
151140

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

177143
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)