I want to copy my existing custom image from Azure classic storage account to Azure ARM storage account by using Java. (Implementation of azCopy using Java programming)
问问题
357 次
1 回答
0
听起来您想azCopy
使用 Java 编程来实现下面的 cmdlet。
Blob:跨存储帐户复制单个 Blob
AzCopy /Source:https://sourceaccount.blob.core.windows.net/mycontainer1 /Dest:https://destaccount.blob.core.windows.net/mycontainer2 / SourceKey : key1 /DestKey:key2 /Pattern:abc。文本
作为参考,这是我的代码示例,用于使用依赖于 Java 的Azure Storage Client SDK 的Java 编程实现上述 cmdlet 。
复制的关键方法是CloudBlob.startCopy
。
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());
}
}
于 2016-11-14T08:00:50.107 回答