本文整理汇总了Java中com.google.api.services.storage.model.Objects类的典型用法代码示例。如果您正苦于以下问题:Java Objects类的具体用法?Java Objects怎么用?Java Objects使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Objects类属于com.google.api.services.storage.model包,在下文中一共展示了Objects类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryAdvance
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Override
public boolean tryAdvance(Consumer<? super StorageObject> action) {
try {
// Retrieves the next page of items
Objects objects = SocketAccess.doPrivilegedIOException(list::execute);
if ((objects == null) || (objects.getItems() == null) || (objects.getItems().isEmpty())) {
return false;
}
// Consumes all the items
objects.getItems().forEach(action::accept);
// Sets the page token of the next page,
// null indicates that all items have been consumed
String next = objects.getNextPageToken();
if (next != null) {
list.setPageToken(next);
return true;
}
return false;
} catch (Exception e) {
throw new BlobStoreException("Exception while listing objects", e);
}
}
示例2: setUpGetFilesPage
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
private void setUpGetFilesPage(List<String> objectNames) {
// these are final classes, so use fakes instead of mocks.
List<StorageObject> fakeItems = new ArrayList<>();
for (int i = 0; i < objectNames.size(); ++i) {
StorageObject fakeObject = new StorageObject().setName(objectNames.get(i));
fakeItems.add(fakeObject);
}
Objects listObjects = new Objects().setItems(fakeItems);
try {
when(this.objectList.execute()).thenReturn(listObjects);
} catch (IOException e) {
fail("Failed to setup getFilesPage");
}
}
示例3: retrieve
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
/**
* Retrieves a file from GCS, stores it in the local file system and returns its absolute path.
*/
@Override
public String retrieve(String str) throws IOException, GeneralSecurityException {
File localCopy = new File(GCSConstants.GCS_DIRECTORY + "/" + str + + System.currentTimeMillis());
localCopy.getParentFile().mkdirs();
localCopy.createNewFile();
Storage client = StorageFactory.getService();
Storage.Objects.Get bucketRequest = client.objects().get(GCSConstants.GCS_BUCKET_NAME, toGCSFileName(str));
bucketRequest.getMediaHttpDownloader().setDirectDownloadEnabled(true);
bucketRequest.executeMediaAndDownloadTo(new FileOutputStream(localCopy));
LogUtils.debug(LOG_TAG, "Download complete. Path: " + localCopy.getAbsolutePath());
return localCopy.getAbsolutePath();
}
示例4: uploadFile
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
/**
* Uploads data to an object in a bucket.
*
* @param name the name of the destination object.
* @param contentType the MIME type of the data.
* @param file the file to upload.
* @param bucketName the name of the bucket to create the object in.
*/
private void uploadFile(String name, String contentType, File file, String bucketName) throws IOException,
GeneralSecurityException {
InputStreamContent contentStream = new InputStreamContent(contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.execute();
LogUtils.debug(LOG_TAG, "Successfully uploaded file to bucket.\nName: " + name + "\nBucket name: " +
bucketName);
}
示例5: listObjects
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
/**
* Lists {@link Objects} given the {@code bucket}, {@code prefix}, {@code pageToken}.
*/
public Objects listObjects(String bucket, String prefix, @Nullable String pageToken)
throws IOException {
// List all objects that start with the prefix (including objects in sub-directories).
Storage.Objects.List listObject = storageClient.objects().list(bucket);
listObject.setMaxResults(MAX_LIST_ITEMS_PER_CALL);
listObject.setPrefix(prefix);
if (pageToken != null) {
listObject.setPageToken(pageToken);
}
try {
return ResilientOperation.retry(
ResilientOperation.getGoogleRequestCallable(listObject),
createBackOff(),
RetryDeterminer.SOCKET_ERRORS,
IOException.class);
} catch (Exception e) {
throw new IOException(
String.format("Unable to match files in bucket %s, prefix %s.", bucket, prefix),
e);
}
}
示例6: testNonExistentObjectReturnsEmptyResult
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testNonExistentObjectReturnsEmptyResult() throws IOException {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class);
GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/nonexistentfile");
GoogleJsonResponseException expectedException =
googleJsonResponseException(HttpStatusCodes.STATUS_CODE_NOT_FOUND,
"It don't exist", "Nothing here to see");
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.get(pattern.getBucket(), pattern.getObject())).thenReturn(
mockStorageGet);
when(mockStorageGet.execute()).thenThrow(expectedException);
assertEquals(Collections.EMPTY_LIST, gcsUtil.expand(pattern));
}
示例7: testAccessDeniedObjectThrowsIOException
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testAccessDeniedObjectThrowsIOException() throws IOException {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class);
GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/accessdeniedfile");
GoogleJsonResponseException expectedException =
googleJsonResponseException(HttpStatusCodes.STATUS_CODE_FORBIDDEN,
"Waves hand mysteriously", "These aren't the buckets you're looking for");
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.get(pattern.getBucket(), pattern.getObject())).thenReturn(
mockStorageGet);
when(mockStorageGet.execute()).thenThrow(expectedException);
thrown.expect(IOException.class);
thrown.expectMessage("Unable to get the file object for path");
gcsUtil.expand(pattern);
}
示例8: testFileSizeNonBatch
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testFileSizeNonBatch() throws Exception {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class);
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.get("testbucket", "testobject")).thenReturn(mockStorageGet);
when(mockStorageGet.execute()).thenReturn(
new StorageObject().setSize(BigInteger.valueOf(1000)));
assertEquals(1000, gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject")));
}
示例9: uploadFile
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
/**
* Uploads data to an object in a bucket.
*
* @param name the name of the destination object.
* @param contentType the MIME type of the data.
* @param file the file to upload.
* @param bucketName the name of the bucket to create the object in.
*/
public static void uploadFile(
String name, String contentType, File file, String bucketName)
throws IOException, GeneralSecurityException {
InputStreamContent contentStream = new InputStreamContent(
contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.execute();
}
示例10: performDownloadWithRetry
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
private void performDownloadWithRetry(final Executor executor,
final Storage service,
final StorageObjectId obj, final FilePath localName,
final UploadModule module, final TaskListener listener)
throws IOException, InterruptedException, ExecutorException {
Operation a = new Operation() {
public void act()
throws IOException, InterruptedException, ExecutorException {
listener.getLogger().println(module.prefix(
Messages.Download_Downloading(obj.getName(), localName)));
Storage.Objects.Get getObject = service.objects()
.get(obj.getBucket(), obj.getName());
MediaHttpDownloader downloader = getObject.getMediaHttpDownloader();
if (downloader != null) {
downloader.setDirectDownloadEnabled(true);
}
InputStream is = module.executeMediaAsInputStream(getObject);
localName.copyFrom(is);
}
};
RetryStorageOperation
.performRequestWithRetry(executor, a, module.getInsertRetryCount());
}
示例11: testBuild
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testBuild() throws Exception {
MockUploadModule module = new MockUploadModule(executor);
DownloadStep step = new DownloadStep(CREDENTIALS_ID,
"gs://bucket/path/to/object.txt",
"", module);
FreeStyleProject project = jenkins.createFreeStyleProject("testBuild");
// Set up mock to retrieve the object
StorageObject objToGet = new StorageObject();
objToGet.setBucket("bucket");
objToGet.setName("path/to/obj.txt");
executor.when(Storage.Objects.Get.class, objToGet,
MockUploadModule.checkGetObject("path/to/object.txt"));
module.addNextMedia(IOUtils.toInputStream("test", "UTF-8"));
project.getBuildersList().add(step);
FreeStyleBuild build = jenkins.buildAndAssertSuccess(project);
FilePath result = build.getWorkspace().withSuffix("/path/to/obj.txt");
assertTrue(result.exists());
assertEquals("test", result.readToString());
}
示例12: testBuildPrefix
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testBuildPrefix() throws Exception {
MockUploadModule module = new MockUploadModule(executor);
DownloadStep step = new DownloadStep(CREDENTIALS_ID,
"gs://bucket/path/to/object.txt",
"subPath", module);
step.setPathPrefix("path/to/");
FreeStyleProject project = jenkins.createFreeStyleProject("testBuild");
// Set up mock to retrieve the object
StorageObject objToGet = new StorageObject();
objToGet.setBucket("bucket");
objToGet.setName("path/to/obj.txt");
executor.when(Storage.Objects.Get.class, objToGet,
MockUploadModule.checkGetObject("path/to/object.txt"));
module.addNextMedia(IOUtils.toInputStream("test", "UTF-8"));
project.getBuildersList().add(step);
FreeStyleBuild build = jenkins.buildAndAssertSuccess(project);
FilePath result = build.getWorkspace().withSuffix("/subPath/obj.txt");
assertTrue(result.exists());
assertEquals("test", result.readToString());
}
示例13: testBuildMoreComplex
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Test
public void testBuildMoreComplex() throws Exception {
MockUploadModule module = new MockUploadModule(executor);
DownloadStep step = new DownloadStep(CREDENTIALS_ID,
"gs://bucket/download/$BUILD_ID/path/$BUILD_ID/test_$BUILD_ID.txt",
"output", module);
step.setPathPrefix("download/$BUILD_ID/");
FreeStyleProject project = jenkins.createFreeStyleProject("testBuild");
// Set up mock to retrieve the object
StorageObject objToGet = new StorageObject();
objToGet.setBucket("bucket");
objToGet.setName("download/1/path/1/test_1.txt");
executor.when(Storage.Objects.Get.class, objToGet,
MockUploadModule.checkGetObject("download/1/path/1/test_1.txt"));
module.addNextMedia(IOUtils.toInputStream("contents 1", "UTF-8"));
project.getBuildersList().add(step);
FreeStyleBuild build = jenkins.buildAndAssertSuccess(project);
FilePath result = build.getWorkspace()
.withSuffix("/output/path/1/test_1.txt");
assertTrue(result.exists());
assertEquals("contents 1", result.readToString());
}
示例14: createObjects
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
/**
* Create the Objects object that would have been returned from the Cloud.
*
* @param prefix the requested object prefix
* @param names a list of object names that are available
*/
private Objects createObjects(String prefix, List<String> names) {
Objects o = new Objects();
List<StorageObject> items = new ArrayList<StorageObject>();
Set<String> prefixes = new HashSet<String>();
for (String s : names) {
if (!s.startsWith(prefix)) {
continue;
}
String subdirectory[] = s.substring(prefix.length()).split("/");
if (subdirectory.length > 1) {
// This object is nested deeper. Add a subdirectory
prefixes.add(prefix + subdirectory[0]);
} else {
// Add the object
StorageObject objToGet = new StorageObject();
objToGet.setBucket("bucket");
objToGet.setName(s);
items.add(objToGet);
}
}
o.setItems(items);
o.setPrefixes(new ArrayList<String>(prefixes));
return o;
}
示例15: createEmptyObject
import com.google.api.services.storage.model.Objects; //导入依赖的package包/类
@Override
public void createEmptyObject(StorageResourceId resourceId, CreateObjectOptions options)
throws IOException {
Preconditions.checkArgument(
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
Storage.Objects.Insert insertObject = prepareEmptyInsert(resourceId, options);
try {
insertObject.execute();
} catch (IOException ioe) {
if (canIgnoreExceptionForEmptyObject(ioe, resourceId, options)) {
LOG.info(
"Ignoring exception; verified object already exists with desired state.", ioe);
} else {
throw ioe;
}
}
}