如何通过 Java 将 REST API 用于 Azure blob 存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16834056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use REST API for Azure blob storage,with Java
提问by dev2d
I am new to Azure, i am trying to use REST API for uploading image to Azure blob but unable to find any referance/material which explains the process using Java, i found examples using c# but i want to implement functionality using Java.
我是 Azure 的新手,我正在尝试使用 REST API 将图像上传到 Azure blob,但无法找到任何使用 Java 解释该过程的参考/材料,我找到了使用 c# 的示例,但我想使用 Java 实现功能。
回答by David Makogon
I realize you're asking about using the REST API for blobs (which is fully documented here). However: Given that there's a Java SDK built atop that REST API, you should really look at that unless there's some specific functionality you need that's not implemented in the SDK. And even in that case, you're welcome to update the SDK and submit your changes back to the Azure team, as the SDK source is in github.
我知道你问有关使用斑点的REST API(这是完全记录在这里)。但是:鉴于有一个构建在该 REST API 之上的 Java SDK,您应该真正了解它,除非您需要一些特定的功能,而这些功能并未在 SDK 中实现。即使在这种情况下,也欢迎您更新 SDK 并将您的更改提交回 Azure 团队,因为 SDK 源位于github 中。
Here's documentationon using the Java SDK for working with blobs, and here are the download linksfor the language-specific SDKs including Java.
回答by Albert Cheng
Windows Azure SDK for Java calls the REST API under the hood, why not using Java SDK? http://www.windowsazure.com/en-us/develop/java/how-to-guides/blob-storage/#UploadBlob
Windows Azure SDK for Java 在底层调用 REST API,为什么不使用 Java SDK?http://www.windowsazure.com/en-us/develop/java/how-to-guides/blob-storage/#UploadBlob
回答by Mark Rovetta
@VJD I have written some guidance docs to using the Java API, with sample Java code. This ought to be published on MSDN in the next day or so. I will post a link here once it goes live. Here are the links to the entire content.
@VJD 我已经编写了一些使用 Java API 的指导文档,以及示例 Java 代码。这应该在第二天左右发布在 MSDN 上。一旦它上线,我会在这里发布一个链接。这是整个内容的链接。
Controlling Access to Windows Azure Blob Containers with Java
使用 Java 控制对 Windows Azure Blob 容器的访问
Controlling Access to Windows Azure Queues with Java
使用 Java 控制对 Windows Azure 队列的访问
Controlling Access to Windows Azure Tables with Java
使用 Java 控制对 Windows Azure 表的访问
You can control access to blob containers in your storage account in several ways, but the use of stored access policy may be the most flexible. This enables you to give temporary access to clients, without revealing your secret storage account key, and enables you to cancel, extend, or update the type of access without having to redistribute basic SAS strings.
您可以通过多种方式控制对存储帐户中 Blob 容器的访问,但使用存储访问策略可能是最灵活的。这使您能够在不泄露您的秘密存储帐户密钥的情况下向客户端授予临时访问权限,并使您能够取消、扩展或更新访问类型而无需重新分发基本 SAS 字符串。
For example, your application can generate a stored access policy signature for a container using Java code similar to the following.
例如,您的应用程序可以使用类似于以下内容的 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);
}
}
A client can use a class similar to this to upload a blob with metadata.
客户端可以使用与此类似的类来上传带有元数据的 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);
}
}
A client can use a class similar to this to read a blob.
客户端可以使用与此类似的类来读取 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);
}
}