public long getBlobLength(final String containerName, final String blobPath) throws URISyntaxException, StorageException { return getCloudBlobContainer(containerName).getBlockBlobReference(blobPath).getProperties().getLength(); }
String getHash(String remoteFolderName, String filePath) { String fileName = new File(filePath).getName() CloudBlockBlob blob = container.getBlockBlobReference(remoteFolderName+"/"+filePath) if(!blob) return "" blob.downloadAttributes() byte[] hash = Base64.decode(blob.getProperties().getContentMD5()) BigInteger bigInt = new BigInteger(1, hash) return bigInt.toString(16).padLeft(32, '0') }
@Override public long lastModified() throws IOException { return this.blockBlob.getProperties().getLastModified().getTime(); }
@Override public long contentLength() throws IOException { return this.blockBlob.getProperties().getLength(); }
@Override public Properties load() throws IOException { Properties properties = new Properties(); if (exists()) { long length = manifestBlob.getProperties().getLength(); byte[] data = new byte[(int) length]; try { manifestBlob.downloadToByteArray(data, 0); } catch (StorageException e) { throw new IOException(e); } properties.load(new ByteArrayInputStream(data)); } return properties; }
@Override public Properties load() throws IOException { Properties properties = new Properties(); if (exists()) { long length = manifestBlob.getProperties().getLength(); byte[] data = new byte[(int) length]; try { manifestBlob.downloadToByteArray(data, 0); } catch (StorageException e) { throw new IOException(e); } properties.load(new ByteArrayInputStream(data)); } return properties; }
import java.security.MessageDigest import com.microsoft.windowsazure.services.core.storage.utils.Base64; import com.google.common.io.Files String putFile(String remoteFolder, String filePath){ File fileReference = new File (filePath) // the user is already authentificated and the container is not null CloudBlockBlob blob = container.getBlockBlobReference(remoteFolderName+"/"+filePath); FileInputStream fis = new FileInputStream(fileReference) if(blob){ BlobProperties props = blob.getProperties() MessageDigest md5digest = MessageDigest.getInstance("MD5") String md5 = Base64.encode(Files.getDigest(fileReference, md5digest)) props.setContentMD5(md5) blob.setProperties(props) blob.upload(fis, fileReference.length()) return fileReference.getName() }else{ //ErrorHandling return "" } }
private String[] getStrings(CloudBlockBlob blob) throws StorageException, IOException { HashMap<String, String> md = blob.getMetadata(); byte[] nm = new byte[(int) blob.getProperties().getLength()]; blob.downloadToByteArray(nm, 0, null, null, opContext); if (!md.containsKey("encrypt")) { blob.downloadAttributes(); md = blob.getMetadata(); } boolean encrypt = Boolean.parseBoolean(md.get("encrypt")); if (encrypt) { nm = EncryptUtils.decryptCBC(nm); } boolean compress = Boolean.parseBoolean(md.get("lz4Compress")); if (compress) { int size = Integer.parseInt(md.get("size")); nm = CompressionUtils.decompressLz4(nm, size); } String st = new String(nm); return st.split(","); }
public long getBlobLength(final String containerName, final String blobPath) throws URISyntaxException, StorageException { return getCloudBlobContainer(containerName).getBlockBlobReference(blobPath).getProperties().getLength(); }
public static void main(String[] args) { String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=" + args[0] + ";" + "AccountKey=" + args[1]; try { // Retrieve storage account from connection-string. CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString); // Create the blob client. CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); // Get a reference to a container. // The container name must be lower case CloudBlobContainer container = blobClient.getContainerReference(args[2]); CloudBlockBlob kblob = container.getBlockBlobReference("blocks/MTEwMjE4ODc4MjM0MzY3NzM="); kblob.downloadAttributes(); System.out.println(kblob.getProperties().getStandardBlobTier()); System.out.println(kblob.getProperties().getRehydrationStatus()); } catch (Exception e) { // Output the stack trace. e.printStackTrace(); } }
String STORAGE_CONNECTION_STRING_TEMPLATE = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;"; String accountName = "xxxx"; String key = "XXXXXX"; CloudStorageAccount account = CloudStorageAccount.parse(String.format(STORAGE_CONNECTION_STRING_TEMPLATE, accountName, key)); CloudBlobClient client = account.createCloudBlobClient(); CloudBlobContainer container = client.getContainerReference("mycontainer"); container.createIfNotExists(); String externalFileName = "test.tmp"; CloudBlockBlob blob = container.getBlockBlobReference(externalFileName); if (externalFileName.endsWith(".tmp")) { blob.getProperties().setContentType("image/jpeg"); } BlobOutputStream blobOutputStream = blob.openOutputStream(); String fileName = "test.jpg"; InputStream input = new FileInputStream(fileName); int next = -1; while((next = input.read()) != -1) { blobOutputStream.write(next); } blobOutputStream.close(); // missing in your code, but works if missing. input.close();
private Buffer readBlob(String name) throws IOException { try { CloudBlockBlob blob = getBlob(name); if (!blob.exists()) { return null; } long length = blob.getProperties().getLength(); Buffer buffer = Buffer.allocate((int) length); AzureUtilities.readBufferFully(blob, buffer); return buffer; } catch (StorageException e) { throw new IOException(e); } }
private Buffer readBlob(String name) throws IOException { try { CloudBlockBlob blob = getBlob(name); if (!blob.exists()) { return null; } long length = blob.getProperties().getLength(); Buffer buffer = Buffer.allocate((int) length); AzureUtilities.readBufferFully(blob, buffer); return buffer; } catch (StorageException e) { throw new IOException(e); } }
@Override public boolean blockRestored(String id) { try { CloudBlockBlob blob = container.getBlockBlobReference("blocks/" + id); if(!blob.exists()) return true; blob.downloadAttributes(); if (blob.getProperties().getStandardBlobTier().equals(StandardBlobTier.HOT)) { return true; } else { if (blob.getProperties().getRehydrationStatus() == null || blob.getProperties().getRehydrationStatus().equals(RehydrationStatus.UNKNOWN)) { SDFSLogger.getLog().warn("rehydration status unknow for " + id + " will attempt to rehydrate"); blob.uploadStandardBlobTier(StandardBlobTier.HOT); } return false; } } catch (Exception e) { SDFSLogger.getLog().warn("error while checking block [" + id + "] restored", e); return false; } }
@Override protected Map<String, String> doGetMeta(String fullPath) { try { CloudBlockBlob blob = blobContainer.getBlockBlobReference(fullPath); blob.downloadAttributes(); Map<String, String> meta = C.newMap(blob.getMetadata()); meta.put(ISObject.ATTR_CONTENT_TYPE, blob.getProperties().getContentType()); return meta; } catch (Exception e) { throw E.unexpected(e); } }
@Override protected void doPut(String fullPath, ISObject stuff, Map<String, String> attrs) { try { CloudBlockBlob blob = blobContainer.getBlockBlobReference(fullPath); if (!(stuff instanceof SObject.InputStreamSObject)) { blob.upload(stuff.asInputStream(), stuff.getLength()); } BlobProperties props = blob.getProperties(); // content-type contains "-" which is illegal character in C# identifier // so we have to remove it from meta map String contentType = attrs.remove(ISObject.ATTR_CONTENT_TYPE); if (S.notBlank(contentType)) { props.setContentType(contentType); } blob.uploadProperties(); blob.getMetadata().putAll(attrs); blob.uploadMetadata(); } catch (Exception e) { throw E.unexpected(e); } }
@Override String getStateDebug() { String retval = "uninitialized"; try { this.blob.downloadAttributes(); BlobProperties props = this.blob.getProperties(); retval = props.getLeaseState().toString() + " " + props.getLeaseStatus().toString() + " " + props.getLeaseDuration().toString(); } catch (StorageException e) { retval = "downloadAttributes on the blob caught " + e.toString(); } return retval; } }
@Test public void testBlobAttributesEncryption() throws URISyntaxException, StorageException, IOException { this.blob.downloadAttributes(); assertTrue(this.blob.getProperties().isServerEncrypted()); CloudBlockBlob testBlob = this.container.getBlockBlobReference(this.blob.getName()); testBlob.downloadText(); assertTrue(testBlob.getProperties().isServerEncrypted()); }
@Test public void testEmpty() throws URISyntaxException, StorageException, IOException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName); BlobOutputStream str = blockBlob.openOutputStream(); str.close(); CloudBlockBlob blockBlob2 = this.container.getBlockBlobReference(blobName); blockBlob2.downloadAttributes(); assertEquals(0, blockBlob2.getProperties().getLength()); ArrayList<BlockEntry> blocks = blockBlob2.downloadBlockList(BlockListingFilter.ALL, null, null, null); assertEquals(0, blocks.size()); }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); final int length = 2 * 1024 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); BlobRequestOptions options = new BlobRequestOptions(); options.setSingleBlobPutThresholdInBytes(1024*1024); options.setDisableContentMD5Validation(true); options.setStoreBlobContentMD5(false); blockBlobRef.upload(srcStream, -1, null, options, null); options.setDisableContentMD5Validation(false); options.setStoreBlobContentMD5(true); options.setUseTransactionalContentMD5(true); final CloudBlockBlob blockBlobRef2 = this.container.getBlockBlobReference(blockBlobName); blockBlobRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null); assertNull(blockBlobRef2.getProperties().getContentMD5()); }