/** * Returns a reference to a {@link CloudBlockBlob} object that represents a block blob in this container, using the * specified snapshot ID. * * @param blobName * A <code>String</code> that represents the name of the blob. * @param snapshotID * A <code>String</code> that represents the snapshot ID of the blob. * * @return A {@link CloudBlockBlob} object that represents a reference to the specified block blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudBlockBlob getBlockBlobReference(final String blobName, final String snapshotID) throws URISyntaxException, StorageException { return new CloudBlockBlob(blobName, snapshotID, this); }
/** * Returns a reference to a {@link CloudBlockBlob} object that represents a block blob in this container, using the * specified snapshot ID. * * @param blobName * A <code>String</code> that represents the name of the blob. * @param snapshotID * A <code>String</code> that represents the snapshot ID of the blob. * * @return A {@link CloudBlockBlob} object that represents a reference to the specified block blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudBlockBlob getBlockBlobReference(final String blobName, final String snapshotID) throws URISyntaxException, StorageException { return new CloudBlockBlob(blobName, snapshotID, this); }
URL sasUrl = new URL("<sas-url>"); try {. CloudBlockBlob blob = new CloudBlockBlob(sasUrl) File source = new File(filePath); blob.upload(new FileInputStream(source), source.length()); } catch (Exception e) { // Output the stack trace. e.printStackTrace(); }
CloudBlockBlob blob = new CloudBlockBlob(new URI(sasURL)); File source = new File(filePath); blob.upload(new FileInputStream(source), source.length());
public static CloudBlockBlob createBlockBlobClient(BlobServiceConfiguration cfg) throws Exception { CloudBlockBlob client = (CloudBlockBlob) getConfiguredClient(cfg); if (client == null) { URI uri = prepareStorageBlobUri(cfg); StorageCredentials creds = getAccountCredentials(cfg); client = new CloudBlockBlob(uri, creds); } return client; }
/** * Returns a reference to a {@link CloudBlockBlob} object that represents a block blob in this directory, using the * specified snapshot ID. * * @param blobName * A <code>String</code> that represents the name of the blob. * @param snapshotID * A <code>String</code> that represents the snapshot ID of the blob. * * @return A {@link CloudBlockBlob} object that represents a reference to the specified block blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudBlockBlob getBlockBlobReference(final String blobName, final String snapshotID) throws URISyntaxException, StorageException { Utility.assertNotNullOrEmpty("blobName", blobName); return new CloudBlockBlob(this.getPrefix().concat(blobName), snapshotID, this.getContainer()); }
/** * Returns a reference to a {@link CloudBlockBlob} object that represents a block blob in this directory, using the * specified snapshot ID. * * @param blobName * A <code>String</code> that represents the name of the blob. * @param snapshotID * A <code>String</code> that represents the snapshot ID of the blob. * * @return A {@link CloudBlockBlob} object that represents a reference to the specified block blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudBlockBlob getBlockBlobReference(final String blobName, final String snapshotID) throws URISyntaxException, StorageException { Utility.assertNotNullOrEmpty("blobName", blobName); return new CloudBlockBlob(this.getPrefix().concat(blobName), snapshotID, this.getContainer()); }
CloudBlockBlob blob = new CloudBlockBlob(blobURI);
@Override public CloudBlobWrapper getBlockBlobReference(String relativePath) throws URISyntaxException, StorageException { try { CloudBlockBlob blob = (sasKeyGenerator!=null) ? new CloudBlockBlob(sasKeyGenerator.getRelativeBlobSASUri(storageAccount, getName(), relativePath)) : container.getBlockBlobReference(relativePath); blob.getServiceClient().setDefaultRequestOptions( container.getServiceClient().getDefaultRequestOptions()); return new SASCloudBlockBlobWrapperImpl(blob); } catch (SASKeyGenerationException sasEx) { String errorMsg = "Encountered SASKeyGeneration exception while " + "generating SAS Key for relativePath : " + relativePath + " inside container : " + getName() + " Storage account : " + storageAccount; LOG.error(errorMsg); throw new StorageException(SAS_ERROR_CODE, errorMsg, sasEx); } }
@Test public void testApiVersion() throws InvalidKeyException, StorageException, URISyntaxException { SharedAccessBlobPolicy sp1 = createSharedAccessPolicy( EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 3600); String sas = this.blob.generateSharedAccessSignature(sp1, null); // should not be appended before signing assertEquals(-1, sas.indexOf(Constants.QueryConstants.API_VERSION)); OperationContext ctx = new OperationContext(); ctx.getResponseReceivedEventHandler().addListener(new StorageEvent<ResponseReceivedEvent>() { @Override public void eventOccurred(ResponseReceivedEvent eventArg) { // should be appended after signing HttpURLConnection conn = (HttpURLConnection) eventArg.getConnectionObject(); assertTrue(conn.getURL().toString().indexOf(Constants.QueryConstants.API_VERSION) != -1); } }); CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?" + sas)); sasBlob.uploadMetadata(null, null, ctx); }
@Test public void testBlobUriOnlyConstructors() throws URISyntaxException, StorageException, InvalidKeyException { URI blobURI = new URI(container.getUri().toString() + "/anonblob"); CloudBlockBlob blob = new CloudBlockBlob(blobURI); assertEquals("anonblob", blob.getName()); assertNotNull(blob.getServiceClient()); assertEquals(StorageCredentialsAnonymous.class, blob.getServiceClient().getCredentials().getClass()); blob = container.getBlockBlobReference("anonblob"); String sas = blob.generateSharedAccessSignature(null, "dummyPolicy"); blobURI = new URI(container.getUri().toString() + "/anonblob?" + sas); blob = new CloudBlockBlob(blobURI); assertEquals("anonblob", blob.getName()); assertNotNull(blob.getServiceClient()); assertEquals(StorageCredentialsSharedAccessSignature.class, blob.getServiceClient().getCredentials().getClass()); }
public static CloudBlockBlob defiddler(CloudBlockBlob blob) throws URISyntaxException, StorageException { URI oldUri = blob.getUri(); URI newUri = defiddler(oldUri); if (newUri != oldUri) { CloudBlockBlob newBlob = new CloudBlockBlob(newUri, blob.getServiceClient().getCredentials()); newBlob.setSnapshotID(blob.snapshotID); return newBlob; } else { return blob; } }
@Test @Category(SlowTests.class) public void testBlobSaS() throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException { SharedAccessBlobPolicy sp = createSharedAccessPolicy( EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST), 3600); BlobContainerPermissions perms = new BlobContainerPermissions(); perms.getSharedAccessPolicies().put("readperm", sp); this.container.uploadPermissions(perms); Thread.sleep(30000); CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?" + this.blob.generateSharedAccessSignature(null, "readperm"))); sasBlob.download(new ByteArrayOutputStream()); // do not give the client and check that the new blob's client has the correct perms CloudBlob blobFromUri = new CloudBlockBlob(PathUtility.addToQuery(this.blob.getStorageUri(), this.blob.generateSharedAccessSignature(null, "readperm"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromUri.getServiceClient() .getCredentials().getClass().toString()); // create credentials from sas StorageCredentials creds = new StorageCredentialsSharedAccessSignature( this.blob.generateSharedAccessSignature(null, "readperm")); CloudBlobClient bClient = new CloudBlobClient(sasBlob.getServiceClient().getStorageUri(), creds); CloudBlockBlob blobFromClient = bClient.getContainerReference(this.blob.getContainer().getName()) .getBlockBlobReference(this.blob.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromClient.getServiceClient() .getCredentials().getClass().toString()); assertEquals(bClient, blobFromClient.getServiceClient()); }
public static CloudBlob getBlobReference(BlobType type, StorageCredentials credentials, URI uri) throws StorageException, URISyntaxException { CloudBlob blob = null; switch (type) { case APPEND_BLOB: blob = new CloudAppendBlob(credentials.transformUri(uri)); break; case BLOCK_BLOB: blob = new CloudBlockBlob(credentials.transformUri(uri)); break; case PAGE_BLOB: blob = new CloudPageBlob(credentials.transformUri(uri)); break; case UNSPECIFIED: fail(); } return blob; }
headers.setContentType("text/html; charset=utf-8"); CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?" + this.blob.generateSharedAccessSignature(null, headers, "readperm"))); OperationContext context = new OperationContext();
@Override public CloudBlob preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_CREATED) { this.setNonExceptionedRetryableFailure(true); return null; } CloudBlob snapshot = null; final String snapshotTime = BlobResponse.getSnapshotTime(this.getConnection()); if (blob instanceof CloudBlockBlob) { snapshot = new CloudBlockBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } else if (blob instanceof CloudPageBlob) { snapshot = new CloudPageBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } else if (blob instanceof CloudAppendBlob) { snapshot = new CloudAppendBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } snapshot.setProperties(blob.properties); // use the specified metadata if not null : otherwise blob's metadata snapshot.setMetadata(metadata != null ? metadata : blob.metadata); snapshot.updateEtagAndLastModifiedFromResponse(this.getConnection()); return snapshot; } };
@Override public CloudBlob preProcessResponse(CloudBlob blob, CloudBlobClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_CREATED) { this.setNonExceptionedRetryableFailure(true); return null; } CloudBlob snapshot = null; final String snapshotTime = BlobResponse.getSnapshotTime(this.getConnection()); if (blob instanceof CloudBlockBlob) { snapshot = new CloudBlockBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } else if (blob instanceof CloudPageBlob) { snapshot = new CloudPageBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } else if (blob instanceof CloudAppendBlob) { snapshot = new CloudAppendBlob(blob.getName(), snapshotTime, CloudBlob.this.getContainer()); } snapshot.setProperties(blob.properties); // use the specified metadata if not null : otherwise blob's metadata snapshot.setMetadata(metadata != null ? metadata : blob.metadata); snapshot.updateEtagAndLastModifiedFromResponse(this.getConnection()); return snapshot; } };
copySource = new CloudBlockBlob(credentials.transformUri(source.getUri())); copyDestination = new CloudBlockBlob(credentials.transformUri(destination.getUri()));
CloudBlockBlob copySas = new CloudBlockBlob(new URI(copy.getUri().toString() + "?" + sasToken));
assertEquals(endpoint, blockBlob.getServiceClient().getStorageUri()); blockBlob = new CloudBlockBlob(blobUri, client.getCredentials()); assertEquals(blobUri, blockBlob.getStorageUri()); assertEquals(blobUri.getPrimaryUri(), blockBlob.getUri());