1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| using Microsoft.Azure.Storage; using Microsoft.Azure.Storage.Blob; using Microsoft.Azure.Storage.File; using System; using System.IO; using System.Threading.Tasks;
namespace ConsoleApp1 { class Program { static async Task Main() { var appSettings = System.Configuration.ConfigurationManager.AppSettings; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(appSettings["StorageConnectionString"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
String containerName = "myContainer"; String blobName = "xxx.ext"; String localFilePath = "localFile.ext"; var fileStream = File.OpenRead(localFilePath);
upload_to_blob(blobClient, containerName, blobName, fileStream); delete_from_blob(blobClient, containerName, blobName); byte[] bb = readbytes_from_blob(blobClient, containerName, blobName); String text = readtext_from_blob(blobClient, containerName, blobName);
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
String shareName = "myShare"; String dirName = "subDir"; String cloudFileName = "xxx.ext"; String localFileName = "localFile.ext"; var fileStream = System.IO.File.OpenRead(localFileName);
upload_to_fileshare(fileClient, shareName, dirName, cloudFileName, fileStream); delete_from_fileshare(fileClient, shareName, dirName, cloudFileName); byte[] bb = readbytes_from_fileshare(fileClient, shareName, dirName, cloudFileName); String text = readtext_from_fileshare(fileClient, shareName, dirName, cloudFileName);
}
public static void upload_to_blob(CloudBlobClient blobClient, String containerName, String blobName, Stream stream) { CloudBlobContainer container = blobClient.GetContainerReference(containerName); container.CreateIfNotExists(); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.UploadFromStream(stream); }
public static void delete_from_blob(CloudBlobClient blobClient, String containerName, String blobName) { CloudBlobContainer container = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.DeleteIfExists(); }
public static byte[] readbytes_from_blob(CloudBlobClient blobClient, String containerName, String blobName) { CloudBlobContainer container = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
if (!blockBlob.Exists()) return null;
byte[] content = new byte[blockBlob.Properties.Length]; blockBlob.DownloadToByteArray(content, 0); return content; }
public static String readtext_from_blob(CloudBlobClient blobClient, String containerName, String blobName) { CloudBlobContainer container = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
if (!blockBlob.Exists()) return null;
return blockBlob.DownloadText(); }
public static void upload_to_fileshare(CloudFileClient fileClient, String shareName, String dirName, String cloudFileName, Stream stream) { CloudFileShare share = fileClient.GetShareReference(shareName); share.CreateIfNotExists();
CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory webDir = rootDir.GetDirectoryReference(dirName); webDir.CreateIfNotExists();
CloudFile cloudFile = webDir.GetFileReference(cloudFileName); cloudFile.UploadFromStream(stream); }
public static void delete_from_fileshare(CloudFileClient fileClient, String shareName, String dirName, String cloudFileName) { CloudFileShare share = fileClient.GetShareReference(shareName);
CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory webDir = rootDir.GetDirectoryReference(dirName);
CloudFile cloudFile = webDir.GetFileReference(cloudFileName); cloudFile.DeleteIfExists(); }
public static byte[] readbytes_from_fileshare(CloudFileClient fileClient, String shareName, String dirName, String cloudFileName) { CloudFileShare share = fileClient.GetShareReference(shareName); CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFile cloudFile; if (String.IsNullOrEmpty(dirName)) { cloudFile = rootDir.GetFileReference(cloudFileName); } else { CloudFileDirectory webDir = rootDir.GetDirectoryReference(dirName); cloudFile = webDir.GetFileReference(cloudFileName); }
if (!cloudFile.Exists()) return null;
byte[] content = new byte[cloudFile.Properties.Length]; cloudFile.DownloadToByteArray(content, 0); return content; }
public static String readtext_from_fileshare(CloudFileClient fileClient, String shareName, String dirName, String cloudFileName) { CloudFileShare share = fileClient.GetShareReference(shareName); CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFile cloudFile; if (String.IsNullOrEmpty(dirName)) { cloudFile = rootDir.GetFileReference(cloudFileName); } else { CloudFileDirectory webDir = rootDir.GetDirectoryReference(dirName); cloudFile = webDir.GetFileReference(cloudFileName); }
if (!cloudFile.Exists()) return null;
return cloudFile.DownloadText(); }
} }
|