本文整理汇总了Java中com.google.api.services.storage.model.StorageObject类的典型用法代码示例。如果您正苦于以下问题:Java StorageObject类的具体用法?Java StorageObject怎么用?Java StorageObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StorageObject类属于com.google.api.services.storage.model包,在下文中一共展示了StorageObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: blobExists
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
/**
* Returns true if the blob exists in the bucket
*
* @param blobName name of the blob
* @return true if the blob exists, false otherwise
*/
boolean blobExists(String blobName) throws IOException {
try {
StorageObject blob = SocketAccess.doPrivilegedIOException(() -> client.objects().get(bucket, blobName).execute());
if (blob != null) {
return Strings.hasText(blob.getId());
}
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if ((e.getStatusCode() == HTTP_NOT_FOUND) || ((error != null) && (error.getCode() == HTTP_NOT_FOUND))) {
return false;
}
throw e;
}
return false;
}
示例2: tryAdvance
import com.google.api.services.storage.model.StorageObject; //导入依赖的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);
}
}
示例3: recursiveGCSSync
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
private void recursiveGCSSync(File repo, String prefix, Storage gcs) throws IOException {
for (String name : repo.list()) {
if (!name.equals(".git")) {
File file = new File(repo.getPath(), name);
if (file.isDirectory()) {
recursiveGCSSync(file, prefix + file.getName() + "/", gcs);
} else {
InputStream inputStream = new FileInputStream(repo.getPath() + "/" + file.getName());
InputStreamContent mediaContent = new InputStreamContent("text/plain", inputStream);
mediaContent.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
.setName(prefix + file.getName());
gcs.objects().insert(bucket, objectMetadata, mediaContent).execute();
}
}
}
}
示例4: setUpGetFilesPage
import com.google.api.services.storage.model.StorageObject; //导入依赖的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");
}
}
示例5: uploadFile
import com.google.api.services.storage.model.StorageObject; //导入依赖的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);
}
示例6: create
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
/**
* Same as {@link GcsUtil#create(GcsPath, String)} but allows overriding
* {code uploadBufferSizeBytes}.
*/
public WritableByteChannel create(GcsPath path, String type, Integer uploadBufferSizeBytes)
throws IOException {
GoogleCloudStorageWriteChannel channel = new GoogleCloudStorageWriteChannel(
executorService,
storageClient,
new ClientRequestHelper<StorageObject>(),
path.getBucket(),
path.getObject(),
AsyncWriteChannelOptions.newBuilder().build(),
new ObjectWriteConditions(),
Collections.<String, String>emptyMap(),
type);
if (uploadBufferSizeBytes != null) {
channel.setUploadBufferSize(uploadBufferSizeBytes);
}
channel.initialize();
return channel;
}
示例7: testFileSizeNonBatch
import com.google.api.services.storage.model.StorageObject; //导入依赖的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")));
}
示例8: uploadCache
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
@Override
public void uploadCache(JobOutputProcessor outputProcessor, String cachePath) {
try {
outputProcessor.output("Uploading cache file " + cacheFileName + " to google storage\n");
Storage client = createClient();
File uploadFile = new File(cachePath);
InputStreamContent contentStream = new InputStreamContent(
null, new FileInputStream(uploadFile));
contentStream.setLength(uploadFile.length());
StorageObject objectMetadata = new StorageObject()
.setName(cacheFileName);
Storage.Objects.Insert insertRequest = client.objects().insert(
settings.bucketName, objectMetadata, contentStream);
insertRequest.execute();
outputProcessor.output("Cache uploaded\n");
} catch (GeneralSecurityException | IOException e) {
outputProcessor.output("Error upload cache: " + e.getMessage() + "\n");
}
}
示例9: saveImage
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
/**
* Method to insert a image into the bucket of the cloud storage.
*
* @author <a href="mailto:[email protected]"> João Felipe de Medeiros Moreira </a>
* @since 15/10/2015
*
* @param name of the image.
* @param contentStream to be converted.
*
* @return the media link of the image.
*
* @throws IOException in case a IO problem.
* @throws GeneralSecurityException in case a security problem.
*/
public static String saveImage(String name, InputStreamContent contentStream)
throws IOException, GeneralSecurityException {
logger.finest("###### Saving a image");
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(ALL_USERS).setRole(READER)));
Storage client = getService();
String bucketName = getBucket().getName();
Storage.Objects.Insert insertRequest =
client.objects().insert(bucketName, objectMetadata, contentStream);
return insertRequest.execute().getMediaLink();
}
示例10: closeCurrentUpload
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
private void closeCurrentUpload()
{
try {
if (currentUpload != null) {
StorageObject obj = currentUpload.get();
storageObjects.add(obj);
logger.info("Uploaded '{}/{}' to {}bytes", obj.getBucket(), obj.getName(), obj.getSize());
currentUpload = null;
}
callCount = 0;
}
catch (InterruptedException | ExecutionException ex) {
throw Throwables.propagate(ex);
}
}
示例11: startUpload
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
private Future<StorageObject> startUpload(final String path)
{
try {
final ExecutorService executor = Executors.newCachedThreadPool();
final String hash = getLocalMd5hash(tempFile.getAbsolutePath());
return executor.submit(new Callable<StorageObject>() {
@Override
public StorageObject call() throws IOException
{
try {
logger.info("Uploading '{}/{}'", bucket, path);
return execUploadWithRetry(path, hash);
}
finally {
executor.shutdown();
}
}
});
}
catch (IOException ex) {
throw Throwables.propagate(ex);
}
}
示例12: uploadFile
import com.google.api.services.storage.model.StorageObject; //导入依赖的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();
}
示例13: undo
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
@Override
public boolean undo() {
// Try to get the previous version. This will be null on the first deploy.
final StorageObject previousObject =
gcsController.getStorageObject(previousTopologyObjectName);
if (previousObject != null) {
try {
gcsController.copyStorageObject(previousTopologyObjectName, topologyObjectName,
previousObject);
} catch (IOException ioe) {
LOG.log(Level.SEVERE, "Reverting to previous topology failed", ioe);
return false;
}
}
return true;
}
示例14: verifyObjectBackedUpIfExists
import com.google.api.services.storage.model.StorageObject; //导入依赖的package包/类
@Test
public void verifyObjectBackedUpIfExists() throws IOException {
// return an object to simulate that the topology has been uploaded before
final StorageObject currentStorageObject = createStorageObject(topologyObjectName);
Mockito.when(mockGcsController
.getStorageObject(Mockito.matches(topologyObjectName)))
.thenReturn(currentStorageObject);
// return an object when we try to create one
Mockito.when(mockGcsController
.createStorageObject(Mockito.matches(topologyObjectName), Mockito.any(File.class)))
.thenReturn(createStorageObject(topologyObjectName));
uploader.initialize(createDefaultBuilder().build());
uploader.uploadPackage();
// verify that we copied the old topology before uploading the new one
Mockito.verify(mockGcsController)
.copyStorageObject(topologyObjectName, previousTopologyObjectName,
currentStorageObject);
}
示例15: testBuild
import com.google.api.services.storage.model.StorageObject; //导入依赖的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());
}