如何使用Java将图像从Azure存储帐户复制到ARM存储帐户?

时间:2016-11-12 07:32:23

标签: java storage azure-storage-blobs azure-resource-manager azcopy

我想使用Java将我现有的自定义映像从Azure经典存储帐户复制到Azure ARM存储帐户。 (使用Java编程实现 azCopy

1 个答案:

答案 0 :(得分:0)

听起来您想使用Java编程实现下面的import java.net.URI; import java.net.URISyntaxException; import java.security.InvalidKeyException; import java.util.HashMap; import java.util.Map; import com.microsoft.azure.storage.StorageCredentials; import com.microsoft.azure.storage.StorageException; import com.microsoft.azure.storage.StorageUri; import com.microsoft.azure.storage.blob.CloudBlob; import com.microsoft.azure.storage.blob.CloudBlockBlob; public class AzCopySample { private static final String STORAGE_CONNECTION_STRING_TEMPLATE = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;"; public static CloudBlob genBlobReference(Map<String, String> cmdOptions, String option) throws URISyntaxException, InvalidKeyException, StorageException { URI uri = new URI(cmdOptions.get(option)); String host = uri.getHost(); String account = host.substring(0, host.indexOf(".")); String key = cmdOptions.get(option+"Key"); String connectionString = String.format(STORAGE_CONNECTION_STRING_TEMPLATE, account, key); StorageCredentials credentials = StorageCredentials.tryParseCredentials(connectionString); StorageUri storageUri = new StorageUri(uri); CloudBlob blob = new CloudBlockBlob(storageUri, credentials); return blob; } public static void main(String[] args) throws URISyntaxException, InvalidKeyException, StorageException { String command = "AzCopy /Source:https://sourceaccount.blob.core.windows.net/mycontainer1 /Dest:https://destaccount.blob.core.windows.net/mycontainer2 /SourceKey:key1 /DestKey:key2 /Pattern:abc.txt"; String[] cmdItems = command.split(" "); Map<String, String> cmdOptions = new HashMap<>(); for(int i = 1; i < cmdItems.length; i++) { String cmdItem = cmdItems[i]; int index = cmdItem.indexOf(":"); String key = cmdItem.substring(0, index); String value = cmdItem.substring(index+1); cmdOptions.put(key, value); } CloudBlob source = genBlobReference(cmdOptions, "/Source"); CloudBlob dest = genBlobReference(cmdOptions, "/Dest"); dest.startCopy(source.getUri()); } } cmdlet。

  

Blob:Copy single blob across Storage accounts

     

AzCopy /来源:https://sourceaccount.blob.core.windows.net/mycontainer1 / Dest:https://destaccount.blob.core.windows.net/mycontainer2 / SourceKey:key1 / DestKey:key2 /Pattern:abc.txt

作为参考,这是我使用Java编程实现上述cmdlet的代码示例,它取决于Azure Storage Client SDK for Java

复制的关键方法是CloudBlob.startCopy

{{1}}