2017-02-25 70 views
0

下面是我的代碼使用文件屬性

com.microsoft.azure.storage

庫文件上傳到在Azure Blob存儲

public class BlobUploader { 
    private CloudBlobContainer blobContainer; 
    private static Logger LOGGER = LoggerFactory.getLogger(BlobUploader.class); 

    /** 
    * Constructor of the BlobUploader 
    * 
    * @param storageAccountName The storage account name where the files will be uploaded to. 
    * @param storageAccountKey The storage account key of the storage account 
    * @param containerName The container name where the files will be uploaded to. 
    */ 
    public BlobUploader(String storageAccountName, String storageAccountKey, String containerName) { 

     String storageConnectionString = "DefaultEndpointsProtocol=http;AccountName=" + storageAccountName + ";AccountKey=" + storageAccountKey; 

     CloudStorageAccount storageAccount; 
     try { 
      storageAccount = CloudStorageAccount.parse(storageConnectionString); 
      CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); 
      // Retrieve reference to a previously created container. 
      this.blobContainer = blobClient.getContainerReference(containerName); 
     } catch (Exception e) { 
      LOGGER.error("failed to construct blobUploader", e); 
     } 
    } 

    public void upload(String filePath) throws Exception { 

     // Define the path to blob in the container 
     String blobPath = "/uploads"; 
     File fileToBeUploaded = new File(filePath); 
     String fileName = fileToBeUploaded.getName(); 

     String blobName = blobPath + fileName; 

     // Create or overwrite the blob with contents from a local file. 
     CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName); 

     System.out.println("start uploading file " + filePath + " to blob " + blobName); 

     blob.upload(new FileInputStream(fileToBeUploaded), fileToBeUploaded.length()); 
     System.out.println("upload succeeded."); 
    } 
} 

我找了一個API,其中,給上傳到Azure的Blob存儲的文件的文件路徑,它可以返回我的屬性是文件,具體的日期和時間上傳。

在Java中是否有API支持?

回答

2

我找了一個API,其中,給定一個文件路徑文件上傳到 在Azure Blob存儲,它可以返回我,文件的屬性, 具體而言,上傳的日期和時間。

你正在尋找的方法是downloadAttributes()其中返回BlobProperties類型的對象將設置是BlobProperties類型的斑點的屬性。它將包含關於blob的信息。你想在那裏使用的方法是getLastModified()

但是,這將返回上次更新blob時的日期/時間。因此,如果您創建了一個blob並且不做任何更改,則可以使用此屬性來查明它的上傳時間。但是,如果在創建塊後(如設置屬性/元數據等)對塊進行了任何更改,則返回的值是上次更改時的日期/時間。

如果您有興趣瞭解創建blob的時間,可能需要將此信息作爲自定義元數據與blob一起存儲。

您可以在這裏獲得有關SDK的詳細信息:http://azure.github.io/azure-storage-java/

+0

這是有益的,謝謝。我對上傳時間很感興趣,所以元數據並不是真正需要的,但是要感謝這些信息。小修正:downloadAttributes()的返回類型是void,它不會顯式返回BlobProperties類型的對象,儘管在內部情況可能如此。所以你做blob.downloadAttributes();其次是System.out.println(blob.getProperties()。getLastModified()); – saltmangotree

+0

謝謝你指出我的錯誤。我糾正了我的答案。 –

+0

在我們完成之前還有一個問題 - 我能夠獲得文件DirA/DirB/file.csv的屬性,但無法獲得類似的目錄屬性,比如DirA或DirB。目錄是不是blob?我怎樣才能獲得類似的目錄屬性? – saltmangotree