Skip to content

Commit 04aa874

Browse files
author
Sandeep Kumawat
committed
Introduce interface changes to read/write blob with object metadata
Signed-off-by: Sandeep Kumawat <skumwt@amazon.com>
1 parent a103b84 commit 04aa874

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

server/src/main/java/org/opensearch/common/blobstore/BlobContainer.java

+57
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public interface BlobContainer {
7777
*/
7878
InputStream readBlob(String blobName) throws IOException;
7979

80+
/**
81+
* Creates a new {@link BlobDownloadResponse} for the given blob name.
82+
*
83+
* @param blobName
84+
* The name of the blob to get an {@link InputStream} for.
85+
* @return The {@code InputStream} to read the blob.
86+
* @throws NoSuchFileException if the blob does not exist
87+
* @throws IOException if the blob can not be read.
88+
*/
89+
default BlobDownloadResponse readBlobWithMetadata(String blobName) throws IOException{
90+
return null;
91+
};
92+
8093
/**
8194
* Creates a new {@link InputStream} that can be used to read the given blob starting from
8295
* a specific {@code position} in the blob. The {@code length} is an indication of the
@@ -128,6 +141,27 @@ default long readBlobPreferredLength() {
128141
*/
129142
void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws IOException;
130143

144+
/**
145+
* Reads blob content from the input stream and writes it to the container in a new blob with the given name, and metadata.
146+
* This method assumes the container does not already contain a blob of the same blobName. If a blob by the
147+
* same name already exists, the operation will fail and an {@link IOException} will be thrown.
148+
*
149+
* @param blobName
150+
* The name of the blob to write the contents of the input stream to.
151+
* @param inputStream
152+
* The input stream from which to retrieve the bytes to write to the blob.
153+
* @param metadata
154+
* The metadata to be associate with the blob upload.
155+
* @param blobSize
156+
* The size of the blob to be written, in bytes. It is implementation dependent whether
157+
* this value is used in writing the blob to the repository.
158+
* @param failIfAlreadyExists
159+
* whether to throw a FileAlreadyExistsException if the given blob already exists
160+
* @throws FileAlreadyExistsException if failIfAlreadyExists is true and a blob by the same name already exists
161+
* @throws IOException if the input stream could not be read, or the target blob could not be written to.
162+
*/
163+
default void writeBlobWithMetadata(String blobName, InputStream inputStream, Map<String, String> metadata, long blobSize, boolean failIfAlreadyExists) throws IOException {};
164+
131165
/**
132166
* Reads blob content from the input stream and writes it to the container in a new blob with the given name,
133167
* using an atomic write operation if the implementation supports it.
@@ -149,6 +183,29 @@ default long readBlobPreferredLength() {
149183
*/
150184
void writeBlobAtomic(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws IOException;
151185

186+
/**
187+
* Reads blob content from the input stream and writes it to the container in a new blob with the given name,and metadata
188+
* using an atomic write operation if the implementation supports it.
189+
* <p>
190+
* This method assumes the container does not already contain a blob of the same blobName. If a blob by the
191+
* same name already exists, the operation will fail and an {@link IOException} will be thrown.
192+
*
193+
* @param blobName
194+
* The name of the blob to write the contents of the input stream to.
195+
* @param inputStream
196+
* The input stream from which to retrieve the bytes to write to the blob.
197+
* @param metadata
198+
* The metadata to be associate with the blob upload.
199+
* @param blobSize
200+
* The size of the blob to be written, in bytes. It is implementation dependent whether
201+
* this value is used in writing the blob to the repository.
202+
* @param failIfAlreadyExists
203+
* whether to throw a FileAlreadyExistsException if the given blob already exists
204+
* @throws FileAlreadyExistsException if failIfAlreadyExists is true and a blob by the same name already exists
205+
* @throws IOException if the input stream could not be read, or the target blob could not be written to.
206+
*/
207+
default void writeBlobAtomicWithMetadata(String blobName, InputStream inputStream, Map<String, String> metadata, long blobSize, boolean failIfAlreadyExists) throws IOException {};
208+
152209
/**
153210
* Deletes this container and all its contents from the repository.
154211
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.blobstore;
10+
11+
import java.io.InputStream;
12+
import java.util.Map;
13+
14+
/**
15+
* A class for blob download response
16+
*
17+
* @opensearch.internal
18+
*/
19+
public class BlobDownloadResponse {
20+
21+
/**
22+
* Downloaded blob InputStream
23+
*/
24+
private InputStream inputStream;
25+
26+
/**
27+
* Metadata of the downloaded blob
28+
*/
29+
private Map<String, String> metadata;
30+
31+
public InputStream getInputStream() {
32+
return inputStream;
33+
}
34+
35+
public Map<String, String> getMetadata() {
36+
return metadata;
37+
}
38+
39+
public BlobDownloadResponse(InputStream inputStream, Map<String, String> metadata){
40+
this.inputStream = inputStream;
41+
this.metadata = metadata;
42+
}
43+
44+
45+
}

server/src/main/java/org/opensearch/index/translog/transfer/BlobStoreTransferService.java

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.opensearch.common.blobstore.BlobMetadata;
1818
import org.opensearch.common.blobstore.BlobPath;
1919
import org.opensearch.common.blobstore.BlobStore;
20+
import org.opensearch.common.blobstore.BlobDownloadResponse;
2021
import org.opensearch.common.blobstore.stream.write.WriteContext;
2122
import org.opensearch.common.blobstore.stream.write.WritePriority;
2223
import org.opensearch.common.blobstore.transfer.RemoteTransferContainer;
@@ -164,6 +165,11 @@ public InputStream downloadBlob(Iterable<String> path, String fileName) throws I
164165
return blobStore.blobContainer((BlobPath) path).readBlob(fileName);
165166
}
166167

168+
@Override
169+
public BlobDownloadResponse downloadBlobWithMetadata(Iterable<String> path, String fileName) throws IOException {
170+
return blobStore.blobContainer((BlobPath) path).readBlobWithMetadata(fileName);
171+
}
172+
167173
@Override
168174
public void deleteBlobs(Iterable<String> path, List<String> fileNames) throws IOException {
169175
blobStore.blobContainer((BlobPath) path).deleteBlobsIgnoringIfNotExists(fileNames);

server/src/main/java/org/opensearch/index/translog/transfer/TransferService.java

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.opensearch.index.translog.transfer;
1010

11+
import org.opensearch.common.blobstore.BlobDownloadResponse;
1112
import org.opensearch.common.blobstore.BlobMetadata;
1213
import org.opensearch.common.blobstore.BlobPath;
1314
import org.opensearch.common.blobstore.stream.write.WritePriority;
@@ -125,6 +126,15 @@ void uploadBlobs(
125126
*/
126127
InputStream downloadBlob(Iterable<String> path, String fileName) throws IOException;
127128

129+
/**
130+
*
131+
* @param path the remote path from where download should be made
132+
* @param fileName the name of the file
133+
* @return {@link BlobDownloadResponse} of the remote file
134+
* @throws IOException the exception while reading the data
135+
*/
136+
BlobDownloadResponse downloadBlobWithMetadata(Iterable<String> path, String fileName) throws IOException;
137+
128138
void listAllInSortedOrder(Iterable<String> path, String filenamePrefix, int limit, ActionListener<List<BlobMetadata>> listener);
129139

130140
void listAllInSortedOrderAsync(

0 commit comments

Comments
 (0)