【发布时间】:2013-05-25 21:41:33
【问题描述】:
我是 Azure 的新手,我正在尝试使用 REST API 将图像上传到 Azure blob,但找不到任何解释使用 Java 的过程的参考/材料,我找到了使用 c# 的示例,但我想使用 Java 实现功能.
【问题讨论】:
标签: java azure azure-storage azure-blob-storage
我是 Azure 的新手,我正在尝试使用 REST API 将图像上传到 Azure blob,但找不到任何解释使用 Java 的过程的参考/材料,我找到了使用 c# 的示例,但我想使用 Java 实现功能.
【问题讨论】:
标签: java azure azure-storage azure-blob-storage
我知道您是在询问是否将 REST API 用于 blob(已完整记录 here)。但是:鉴于在该 REST API 之上构建了一个 Java SDK,您应该真正查看它,除非您需要一些未在 SDK 中实现的特定功能。即使在这种情况下,也欢迎您更新 SDK 并将您的更改提交回 Azure 团队,因为 SDK 源位于 github。
这里是 documentation,关于使用 Java SDK 处理 Blob,这里是 download links,用于包括 Java 在内的特定语言 SDK。
【讨论】:
Windows Azure SDK for Java 在后台调用 REST API,为什么不使用 Java SDK? http://www.windowsazure.com/en-us/develop/java/how-to-guides/blob-storage/#UploadBlob
【讨论】:
@VJD 我已经编写了一些使用 Java API 的指导文档,以及示例 Java 代码。这应该在第二天左右发布在 MSDN 上。一旦它上线,我会在这里发布一个链接。以下是完整内容的链接。
Controlling Access to Windows Azure Blob Containers with Java
Controlling Access to Windows Azure Queues with Java
Controlling Access to Windows Azure Tables with Java
您可以通过多种方式控制对存储帐户中 blob 容器的访问,但使用存储访问策略可能是最灵活的。这使您能够在不泄露您的秘密存储帐户密钥的情况下向客户端授予临时访问权限,并且使您能够取消、扩展或更新访问类型,而无需重新分发基本的 SAS 字符串。
例如,您的应用程序可以使用类似于以下的 Java 代码为容器生成存储的访问策略签名。
public class PolicySAS
{
public static void main(String[] args) throws InvalidKeyException,
URISyntaxException, StorageException
{
Account creds = new Account(); //Account key required to create SAS
final String storageConnectionString = creds.getstorageconnectionstring();
CloudStorageAccount storageAccount =
CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("container1");
container.createIfNotExist();
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
GregorianCalendar calendar =
new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(new Date());
policy.setSharedAccessStartTime(calendar.getTime()); //Immediately applicable
calendar.add(Calendar.HOUR, 3); //Applicable time-span is 3 hours
policy.setSharedAccessExpiryTime(calendar.getTime());
policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ,
SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.DELETE,
SharedAccessBlobPermissions.LIST));
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
//Private container with no access for anonymous users
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
//Name the shared access policy: heath
containerPermissions.getSharedAccessPolicies().put("heath", policy);
container.uploadPermissions(containerPermissions);
//Generate the policy SAS string for heath access
String sas = container.generateSharedAccessSignature(
new SharedAccessBlobPolicy(),"heath");
System.out.println("The stored access policy signature:");
System.out.println(sas);
}
}
客户端可以使用与此类似的类来上传带有元数据的 blob。
public class SASblober
{
public static void main(String[] args) throws URISyntaxException,
FileNotFoundException, StorageException, IOException
{
//This does not reveal the secret storage account key
URI baseuri = new URI("http://grassy.blob.core.windows.net");
CloudBlobClient blobclient = new CloudBlobClient(baseuri);
MyUploadBlob("container1",
"sr=c&sv=2012-02-12&sig=nnPn5P5nnPPnn5Pnn5PPnPPPnPPP5PPPPPP%5PPnn5PPn%55&si=heath",
blobclient);
}
public static void MyUploadBlob(String containerName, String containerSAS,
CloudBlobClient blobClient) throws URISyntaxException, StorageException,
FileNotFoundException, IOException
{
//Uploads a local file to blob-container in cloud
String blobName = "image3.jpg";
String localFileName = "c:\\myimages\\image3.jpg";
URI uri = new URI(blobClient.getEndpoint().toString() + "/" +
containerName + "/" +
blobName +
"?" +
containerSAS);
CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient);
HashMap<String, String> user = new HashMap<String, String>();
user.put("firstname", "Joe");
user.put("lastname", "Brown" );
user.put("age", "28");
user.put("presenter", "no");
sasBlob.setMetadata(user);
File fileReference = new File(localFileName);
sasBlob.upload(new FileInputStream(fileReference), fileReference.length());
System.out.println("The blob: " + blobName + " has been uploaded to:");
System.out.println(uri);
}
}
客户端可以使用与此类似的类来读取 blob。
public class SASread
{
public static void main(String[] args) throws URISyntaxException,
FileNotFoundException, StorageException, IOException
{
URI baseuri = new URI("http://grassy.blob.core.windows.net");
CloudBlobClient blobclient = new CloudBlobClient(baseuri);
MyDownloadBlob("container1",
"sr=c&sv=2012-02-12&sig=nnPn5P5nnPPnn5Pnn5PPnPPPnPPP5PPPPPP%5PPnn5PPn%55&si=heath",
blobclient);
}
public static void MyDownloadBlob(String containerName, String containerSAS,
CloudBlobClient blobClient) throws URISyntaxException, StorageException,
FileNotFoundException, IOException
{
// Downloads blob in cloud to local file
String blobName = "image3.jpg";
String localFileName = "c:\\myoutputimages\\image3.jpg";
URI uri = new URI(blobClient.getEndpoint().toString() + "/" + containerName
+ "/" + blobName + "?" + containerSAS);
CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient);
File fileTarget = new File(localFileName);
sasBlob.download(new FileOutputStream(fileTarget));
HashMap<String, String> user = new HashMap<String, String>();
user = sasBlob.getMetadata();
String name = (user.get("firstname") + " " + user.get("lastname"));
String age = ("age: " + user.get("age"));
String present = ("Presenting talk? " + user.get("presenter"));
System.out.println(name);
System.out.println(age);
System.out.println(present);
System.out.println("The blob at:\n" + uri
+ "\nwas downloaded from the cloud to local file:\n" + localFileName);
}
}
【讨论】: