我是Azure新手,我正在尝试使用REST API将图像上传到Azure blob,但无法找到任何使用Java解释过程的参考/材料,我发现使用c#的示例但我想使用Java实现功能
答案 0 :(得分:1)
我意识到你正在询问如何使用REST API进行blob(完全记录here)。但是:鉴于在REST API上面构建了一个Java SDK,除非你需要一些在SDK中没有实现的特定功能,否则你应该真正看看它。即使在这种情况下,也欢迎您更新SDK并将更改提交回Azure团队,因为SDK源位于github。
以下是使用Java SDK处理blob的documentation,以下是包含Java的特定于语言的SDK的download links。
答案 1 :(得分:1)
Windows Azure SDK for Java调用REST API,为什么不使用Java SDK? http://www.windowsazure.com/en-us/develop/java/how-to-guides/blob-storage/#UploadBlob
答案 2 :(得分:0)
@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);
}
}