最近我個人在使用Azure檔案服務,這篇簡單紀錄使用C#如何存取。
假設大家已經知道如何建立Azure檔案服務,所以我這裡將不會介紹如何建立
分享名稱
目前我這裡有一個子資料夾Upload和一個檔案。
Ps:也可使用 REST APIs 來處理
private string connectionString
{
get
{
return @"DefaultEndpointsProtocol=https;AccountName=" + StorageAccountName +
";AccountKey=" + StorageAccountKey;
}
}
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
上傳
public bool UploadFile(string filepath, string filename,string azurefolder)
{
try
{
CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("demofile");//share name
cloudFileShare.CreateIfNotExists();
CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
bool usefolder = string.IsNullOrWhiteSpace(azurefolder);//check subfolder
CloudFileDirectory fileDirectory;
if (usefolder)
fileDirectory = null;
else
{
fileDirectory = rootDirectory.GetDirectoryReference(azurefolder);
fileDirectory.CreateIfNotExists();
}
CloudFileDirectory finalDirectory = usefolder ? rootDirectory : fileDirectory;
CloudFile cloudFile = finalDirectory.GetFileReference(filename);
cloudFile.UploadFromFile(filepath, System.IO.FileMode.Open);
return true;
}
catch
{
return false;
}
}
Azure檔案服務上的upload資料夾可以看到剛剛上傳的檔案
取得所有檔案
List<AzureFileItem> allFiles = new List<AzureFileItem>();
public List<AzureFileItem> GetListOfFiles()
{
CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("demofile");//share name
cloudFileShare.CreateIfNotExists();
CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
List<string> subfolders = new List<string>();
foreach (IListFileItem fileItem in rootDirectory.ListFilesAndDirectories())
{
try
{
AzureFileItem fi = new AzureFileItem();
CloudFile fileCheck = fileItem as CloudFile;
if (fileCheck == null)//maybe Directory or Other
{
CloudFileDirectory dirCheck = fileItem as CloudFileDirectory;
if (dirCheck == null)
continue;
subfolders.Add(dirCheck.Name);
}
else {
fi.FileName = Path.GetFileName(fileItem.Uri.LocalPath);
fi.FileSize = fileCheck.Properties.Length / 1024;
fi.Path = string.IsNullOrEmpty(rootDirectory.Name) ? "/" : rootDirectory.Name;
allFiles.Add(fi);
}
}
catch
{
}
}
if (subfolders != null && subfolders.Any())
{
GetAllFilesofSubFolder(rootDirectory, subfolders);
}
return allFiles;
}
下載
public Stream DownloadFile(string filename, string azurefolder)
{
try
{
CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("demofile");
cloudFileShare.CreateIfNotExists();
CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
bool usefolder = string.IsNullOrWhiteSpace(azurefolder);//check subfolder
CloudFileDirectory fileDirectory;
if (usefolder)
fileDirectory = null;
else
{
fileDirectory = rootDirectory.GetDirectoryReference(azurefolder);
fileDirectory.CreateIfNotExists();
}
CloudFileDirectory finalDirectory = usefolder ? rootDirectory : fileDirectory;
CloudFile cloudFile = finalDirectory.GetFileReference(filename);
var stream = new MemoryStream();
cloudFile.DownloadToStream(stream);
stream.Seek(0, SeekOrigin.Begin);
//cloudFile.DownloadToFile(Path.Combine(Path.GetTempPath(), filename), FileMode.Create);
return stream;
}
catch
{
return null;
}
}
刪除
public bool DeleteFile(string filename, string azurefolder)
{
try
{
CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("demofile");//share name
cloudFileShare.CreateIfNotExists();
CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
bool usefolder = string.IsNullOrWhiteSpace(azurefolder);//check subfolder
CloudFileDirectory fileDirectory;
if (usefolder)
fileDirectory = null;
else
{
fileDirectory = rootDirectory.GetDirectoryReference(azurefolder);
fileDirectory.CreateIfNotExists();
}
CloudFileDirectory finalDirectory = usefolder ? rootDirectory : fileDirectory;
CloudFile cloudFile = finalDirectory.GetFileReference(filename);
cloudFile.Delete();//say goodbye
return true;
}
catch
{
return false;
}
}
參考