此页面有用吗?
您对此内容的反馈非常重要。 请告诉我们您的想法。
更多反馈?
1500 个剩余字符
导出 (0) 打印
全部展开

在 Java 中创建共享访问签名

更新时间: 2015年2月

以下信息说明如何创建共享访问签名,以及如何配合 Java 使用共享访问签名。

可以向 Blob 或容器应用共享访问签名。以下 Java 代码演示如何为容器创建共享访问签名。此代码使用 0.4.0 版本的存储库。

import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
import java.util.*;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

public class SAS1 
{

    // Substitute your values for the storage account name and key.
    public static final String storageAccountName = "your_account_name";
    public static final String storageAccountKey = " your_account_key";
    
    // Create the storage connection string based on the account name and key.
    public static final String storageConnectionString = 
            "DefaultEndpointsProtocol=http;" + 
            "AccountName=" + storageAccountName + ";" +
            "AccountKey=" + storageAccountKey;

    public static void main(String[] args) 
    {
        try
        {
            CloudStorageAccount storageAccount;

            // Use the connection string to create the storage account.
            storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

            // Create the container if it does not exist.
            // The container name must be lower case.
            CloudBlobContainer container = blobClient.getContainerReference("mysascontainer");
            container.createIfNotExists();
            
            // Create a shared access policy.
            SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
            
            // Create a UTC Gregorian calendar value.
            GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
            
            // Specify the current time as the start time for the shared access signature.
            calendar.setTime(new Date());
            policy.setSharedAccessStartTime(calendar.getTime());
            
            // Use the start time + 1 hour as the end time for the shared access signature. 
            calendar.add(Calendar.HOUR, 1);
            policy.setSharedAccessExpiryTime(calendar.getTime());
            
            // Set READ and WRITE permissions.
            policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE));
            
            
            // Create the container permissions. 
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            
            // Turn off public access.
            containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
            
            // Set the policy using the values set above.
            containerPermissions.getSharedAccessPolicies().put("mypolicy", policy);
            container.uploadPermissions(containerPermissions);
            
            // Create a shared access signature for the container.
            String sas = container.generateSharedAccessSignature(policy, null);
            
            System.out.println("The shared access signature was created.");
        }
        catch (URISyntaxException e) 
        {
            System.out.println("URISyntaxException encountered.");
            e.printStackTrace();
        }
        catch (InvalidKeyException e) 
        {
            System.out.println("InvalidKeyException encountered.");
            e.printStackTrace();
        }
        catch (StorageException e) 
        {
            System.out.println("StorageException encountered.");
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            System.out.println("Exception encountered.");
            e.printStackTrace();
        }


    }

}

Important重要提示
请确保将容器的共享访问签名保密;拥有尚未过期的共享访问签名的任何人都可以访问该容器。同样,拥有尚未过期的 Blob 共享访问签名的任何人都可以访问该 Blob。

除了在容器上创建共享访问签名以外,你还可以使用 CloudBlob.generateSharedAccessSignature 方法为 Blob 创建共享访问签名。

为容器创建共享访问签名后,客户端可以使用它来访问该容器。同样,为 Blob 创建共享访问签名后,客户端可以使用它来访问该 Blob。以下代码演示如何使用容器共享访问签名来上载 Blob。此代码假定你已在容器上创建共享访问签名并已将它安全提供给客户端。容器名称作为第一个参数传入,容器共享访问签名作为第二个参数传入,CloudBlobClient 的实例作为第三个参数传入。

// Create an image, c:\myimages\image1.jpg, prior to running this code.
// Alternatively, change the value used by the FileInputStream constructor 
// to use a different image path and file that you have already created.
public static void MyUploadBlob(String containerName, String containerSAS, CloudBlobClient blobClient)
     throws URISyntaxException, StorageException, FileNotFoundException, IOException
{

    String blobName = "image1.jpg";  // Name as it will appear in blob storage.
    String localFileName = "c:\\myimages\\image1.jpg";  // Path and name of the local file.

    // Append the container name, blob name, and
    // shared access signature to the URI.
    URI uri = new URI(blobClient.getEndpoint().toString() + "/" +
                      containerName + "/" + 
                      blobName + 
                      "?" + 
                      containerSAS);

    // Create a blob using the URI that contains the shared access signature.
    CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient);
    
    // Get a file reference to the local file.
    File fileReference = new File (localFileName);
    
    // Upload the file to the blob.
    sasBlob.upload(new FileInputStream(fileReference), fileReference.length());

}

另请参阅

显示:
© 2015 Microsoft