-
Notifications
You must be signed in to change notification settings - Fork 44
Add GCP Objectstore SDK instead of JClouds #1723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yavor16
wants to merge
3
commits into
master
Choose a base branch
from
adopt-gcp-os-sdk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
...org/cloudfoundry/multiapps/controller/persistence/services/GcpObjectStoreFileStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
package org.cloudfoundry.multiapps.controller.persistence.services; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.channels.Channels; | ||
import java.text.MessageFormat; | ||
import java.time.LocalDateTime; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.api.gax.retrying.RetrySettings; | ||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.ReadChannel; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageException; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.google.cloud.storage.StorageRetryStrategy; | ||
import org.cloudfoundry.multiapps.controller.persistence.Messages; | ||
import org.cloudfoundry.multiapps.controller.persistence.model.FileEntry; | ||
import org.cloudfoundry.multiapps.controller.persistence.util.ObjectStoreUtil; | ||
import org.springframework.http.MediaType; | ||
import org.threeten.bp.Duration; | ||
|
||
public class GcpObjectStoreFileStorage implements FileStorage { | ||
|
||
private final String bucketName; | ||
private final Storage storage; | ||
private static final String BUCKET = "bucket"; | ||
private static final String BASE_64_ENCODED_PRIVATE_KEY_DATA = "base64EncodedPrivateKeyData"; | ||
|
||
public GcpObjectStoreFileStorage(Map<String, Object> credentials) { | ||
this.bucketName = (String) credentials.get(BUCKET); | ||
this.storage = createObjectStoreStorage(credentials); | ||
} | ||
|
||
protected Storage createObjectStoreStorage(Map<String, Object> credentials) { | ||
return StorageOptions.newBuilder() | ||
.setCredentials(getGcpCredentialsSupplier(credentials)) | ||
.setStorageRetryStrategy(StorageRetryStrategy.getDefaultStorageRetryStrategy()) | ||
.setRetrySettings( | ||
RetrySettings.newBuilder() | ||
.setMaxAttempts(6) | ||
.setInitialRetryDelay(Duration.ofMillis(250)) | ||
.setRetryDelayMultiplier(2.0) | ||
.setMaxRetryDelay(Duration.ofSeconds(10)) | ||
.setInitialRpcTimeout(Duration.ofSeconds(60)) | ||
.setRpcTimeoutMultiplier(1.0) | ||
.setMaxRpcTimeout(Duration.ofSeconds(60)) | ||
.setTotalTimeout(Duration.ofMinutes(10)) | ||
.build()) | ||
.build() | ||
.getService(); | ||
} | ||
|
||
private Credentials getGcpCredentialsSupplier(Map<String, Object> credentials) { | ||
if (!credentials.containsKey(BASE_64_ENCODED_PRIVATE_KEY_DATA)) { | ||
return null; | ||
} | ||
byte[] decodedKey = Base64.getDecoder() | ||
.decode((String) credentials.get(BASE_64_ENCODED_PRIVATE_KEY_DATA)); | ||
try { | ||
return GoogleCredentials.fromStream(new ByteArrayInputStream(decodedKey)); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void addFile(FileEntry fileEntry, InputStream content) throws FileStorageException { | ||
BlobId blobId = BlobId.of(bucketName, fileEntry.getId()); | ||
BlobInfo blobInfo = BlobInfo.newBuilder(blobId) | ||
.setContentDisposition(fileEntry.getName()) | ||
.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE) | ||
.setMetadata(ObjectStoreUtil.createFileEntryMetadata(fileEntry)) | ||
.build(); | ||
|
||
putBlob(blobInfo, content); | ||
} | ||
|
||
private void putBlob(BlobInfo blobInfo, InputStream content) throws FileStorageException { | ||
try { | ||
storage.createFrom(blobInfo, content); | ||
} catch (IOException | StorageException e) { | ||
throw new FileStorageException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<FileEntry> getFileEntriesWithoutContent(List<FileEntry> fileEntries) throws FileStorageException { | ||
Set<String> existingFiles = getAllEntries().stream() | ||
.map(Blob::getName) | ||
.collect(Collectors.toSet()); | ||
return fileEntries.stream() | ||
.filter(fileEntry -> !existingFiles.contains(fileEntry.getId())) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public void deleteFile(String id, String space) { | ||
storage.delete(bucketName, id); | ||
} | ||
|
||
@Override | ||
public void deleteFilesBySpaceIds(List<String> spaceIds) { | ||
removeBlobsByFilter(blob -> ObjectStoreUtil.filterBySpaceIds(blob.getMetadata(), spaceIds)); | ||
} | ||
|
||
@Override | ||
public void deleteFilesBySpaceAndNamespace(String space, String namespace) { | ||
removeBlobsByFilter(blob -> ObjectStoreUtil.filterBySpaceAndNamespace(blob.getMetadata(), space, namespace)); | ||
} | ||
|
||
@Override | ||
public int deleteFilesModifiedBefore(LocalDateTime modificationTime) { | ||
return removeBlobsByFilter( | ||
blob -> ObjectStoreUtil.filterByModificationTime(blob.getMetadata(), blob.getName(), modificationTime)); | ||
} | ||
|
||
@Override | ||
public <T> T processFileContent(String space, String id, | ||
FileContentProcessor<T> fileContentProcessor) throws FileStorageException { | ||
FileEntry fileEntry = ObjectStoreUtil.createFileEntry(space, id); | ||
try (InputStream inputStream = openBlobStream(fileEntry)) { | ||
return fileContentProcessor.process(inputStream); | ||
} catch (Exception e) { | ||
throw new FileStorageException(e); | ||
} | ||
} | ||
|
||
private InputStream openBlobStream(FileEntry fileEntry) throws FileStorageException { | ||
Blob blob = getBlob(fileEntry); | ||
return Channels.newInputStream(blob.reader()); | ||
} | ||
|
||
private Blob getBlob(FileEntry fileEntry) throws FileStorageException { | ||
try { | ||
Blob blob = storage.get(bucketName, fileEntry.getId()); | ||
if (blob == null) { | ||
throw new FileStorageException( | ||
MessageFormat.format(Messages.FILE_WITH_ID_AND_SPACE_DOES_NOT_EXIST, | ||
fileEntry.getId(), fileEntry.getSpace())); | ||
} | ||
return blob; | ||
} catch (StorageException e) { | ||
throw new FileStorageException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public InputStream openInputStream(String space, String id) throws FileStorageException { | ||
FileEntry fileEntry = ObjectStoreUtil.createFileEntry(space, id); | ||
return openBlobStream(fileEntry); | ||
} | ||
|
||
@Override | ||
public void testConnection() { | ||
storage.get(bucketName, "test"); | ||
} | ||
|
||
@Override | ||
public void deleteFilesByIds(List<String> fileIds) throws FileStorageException { | ||
removeBlobsByFilter(blob -> fileIds.contains(blob.getName())); | ||
} | ||
|
||
@Override | ||
public <T> T processArchiveEntryContent(FileContentToProcess fileContentToProcess, FileContentProcessor<T> fileContentProcessor) | ||
throws FileStorageException { | ||
FileEntry fileEntry = ObjectStoreUtil.createFileEntry(fileContentToProcess.getSpaceGuid(), fileContentToProcess.getGuid()); | ||
InputStream blobPayload = getBlobPayloadWithOffset(fileEntry, fileContentToProcess.getStartOffset(), | ||
fileContentToProcess.getEndOffset()); | ||
return processContent(fileContentProcessor, blobPayload); | ||
} | ||
|
||
private <T> T processContent(FileContentProcessor<T> fileContentProcessor, InputStream inputStream) | ||
throws FileStorageException { | ||
try { | ||
return fileContentProcessor.process(inputStream); | ||
} catch (IOException e) { | ||
throw new FileStorageException(e); | ||
} | ||
} | ||
|
||
public Set<Blob> getAllEntries() { | ||
return storage.list(bucketName) | ||
.streamAll() | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
protected int removeBlobsByFilter(Predicate<? super Blob> filter) { | ||
List<BlobId> blobIds = getEntryNames(filter).stream() | ||
.map(entry -> BlobId.of(bucketName, entry)) | ||
.toList(); | ||
|
||
if (!blobIds.isEmpty()) { | ||
storage.delete(blobIds); | ||
} | ||
return blobIds.size(); | ||
} | ||
|
||
protected Set<String> getEntryNames(Predicate<? super Blob> filter) { | ||
return storage.list(bucketName) | ||
.streamAll() | ||
.filter(filter) | ||
.map(Blob::getName) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private InputStream getBlobPayloadWithOffset(FileEntry fileEntry, long startOffset, long endOffset) | ||
throws FileStorageException { | ||
try { | ||
Blob blob = getBlob(fileEntry); | ||
ReadChannel reader = storage.reader(blob.getBlobId()); | ||
reader.seek(startOffset); | ||
reader.limit(endOffset + 1); | ||
|
||
return Channels.newInputStream(reader); | ||
} catch (IOException | StorageException e) { | ||
throw new FileStorageException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.