if (metadata.containsKey(uploadId)) { StorageObject storageObject = metadata.get(uploadId); Long generation = storageObject.getGeneration(); if (null == generation) { generation = Long.valueOf(0);
static BlobId fromPb(StorageObject storageObject) { return BlobId.of( storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); } }
/** * Throw if we're asking for generation 0 and the file exists, or if the requested generation * number doesn't match what is asked. * * @param key * @param generationMatch */ private void checkGeneration(String key, Long generationMatch) { if (null == generationMatch) { return; } if (generationMatch == 0 && metadata.containsKey(key)) { throw new StorageException(new FileAlreadyExistsException(key)); } if (generationMatch != 0) { Long generation = metadata.get(key).getGeneration(); if (!generationMatch.equals(generation)) { throw new StorageException( 404, "Generation mismatch. Requested " + generationMatch + " but got " + generation); } } }
private static boolean processedAsFolder( StorageObject so, String delimiter, String prefix, /* inout */ Map<String, StorageObject> folders) { if (delimiter == null) { return false; } int nextSlash = so.getName().indexOf(delimiter, prefix.length()); if (nextSlash < 0) { return false; } String folderName = so.getName().substring(0, nextSlash + 1); if (folders.containsKey(folderName)) { return true; } StorageObject fakeFolder = new StorageObject(); fakeFolder.setName(folderName); fakeFolder.setBucket(so.getBucket()); fakeFolder.setGeneration(so.getGeneration()); folders.put(folderName, fakeFolder); return true; }
.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options))
@Override public byte[] load(StorageObject from, Map<Option, ?> options) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_LOAD); Scope scope = tracer.withSpan(span); try { Storage.Objects.Get getRequest = storage .objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setUserProject(Option.USER_PROJECT.getString(options)); setEncryptionHeaders(getRequest.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.executeMedia().download(out); return out.toByteArray(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } }
private Storage.Objects.Delete deleteCall(StorageObject blob, Map<Option, ?> options) throws IOException { return storage .objects() .delete(blob.getBucket(), blob.getName()) .setGeneration(blob.getGeneration()) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setUserProject(Option.USER_PROJECT.getString(options)); }
ComposeRequest.SourceObjects sourceObject = new ComposeRequest.SourceObjects(); sourceObject.setName(source.getName()); Long generation = source.getGeneration(); if (generation != null) { sourceObject.setGeneration(generation);
private Storage.Objects.Get getCall(StorageObject object, Map<Option, ?> options) throws IOException { return storage .objects() .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setFields(Option.FIELDS.getString(options)) .setUserProject(Option.USER_PROJECT.getString(options)); }
req.target.getName(), req.overrideInfo ? req.target : null) .setSourceGeneration(req.source.getGeneration()) .setRewriteToken(token) .setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall)
static BlobId fromPb(StorageObject storageObject) { return BlobId.of( storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); } }
static BlobId fromPb(StorageObject storageObject) { return BlobId.of(storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); } }
private static boolean processedAsFolder(StorageObject so, String delimiter, String prefix, /* inout */ Map<String, StorageObject> folders) { if (delimiter == null) { return false; } int nextSlash = so.getName().indexOf(delimiter, prefix.length()); if (nextSlash < 0) { return false; } String folderName = so.getName().substring(0, nextSlash + 1); if (folders.containsKey(folderName)) { return true; } StorageObject fakeFolder = new StorageObject(); fakeFolder.setName(folderName); fakeFolder.setBucket(so.getBucket()); fakeFolder.setGeneration(so.getGeneration()); folders.put(folderName, fakeFolder); return true; }
@Override public void onSuccess(StorageObject storageObject, HttpHeaders httpHeaders) throws IOException { final Long generation = storageObject.getGeneration(); Storage.Objects.Delete deleteObject = configureRequest(gcs.objects().delete(bucketName, objectName), bucketName) .setIfGenerationMatch(generation); batchHelper.queue( deleteObject, getDeletionCallback( resourceId, innerExceptions, batchHelper, attempt, generation)); }
@Override public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, long position, int bytes) { try { Get req = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); StringBuilder range = new StringBuilder(); range.append("bytes=").append(position).append("-").append(position + bytes - 1); req.getRequestHeaders().setRange(range.toString()); ByteArrayOutputStream output = new ByteArrayOutputStream(); req.executeMedia().download(output); String etag = req.getLastResponseHeaders().getETag(); return Tuple.of(etag, output.toByteArray()); } catch (IOException ex) { StorageException serviceException = translate(ex); if (serviceException.code() == SC_REQUESTED_RANGE_NOT_SATISFIABLE) { return Tuple.of(null, new byte[0]); } throw serviceException; } }
@Override public byte[] load(StorageObject from, Map<Option, ?> options) { try { Storage.Objects.Get getRequest = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(true); getRequest.executeMediaAndDownloadTo(out); return out.toByteArray(); } catch (IOException ex) { throw translate(ex); } }
private Storage.Objects.Delete deleteRequest(StorageObject blob, Map<Option, ?> options) throws IOException { return storage.objects() .delete(blob.getBucket(), blob.getName()) .setGeneration(blob.getGeneration()) .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); }
private Storage.Objects.Delete deleteCall(StorageObject blob, Map<Option, ?> options) throws IOException { return storage .objects() .delete(blob.getBucket(), blob.getName()) .setGeneration(blob.getGeneration()) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setUserProject(Option.USER_PROJECT.getString(options)); }
private Storage.Objects.Get getRequest(StorageObject object, Map<Option, ?> options) throws IOException { return storage.objects() .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) .setFields(FIELDS.getString(options)); }
private Storage.Objects.Get getCall(StorageObject object, Map<Option, ?> options) throws IOException { return storage .objects() .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setFields(Option.FIELDS.getString(options)) .setUserProject(Option.USER_PROJECT.getString(options)); }