我有一个存储容器,里面有 3 层目录,如下所示:
--Container
--Folder1
--Folder2
--Folder3
--blobs here
Run Code Online (Sandbox Code Playgroud)
我需要检查Folder3 中是否存在任何斑点,或者更好的是检查Folder3 是否存在。我尝试使用
blob_exist = BlobClient.from_connection_string(conn_str = os.getenv("conString"), container_name="Container",blob_name="Folder1/Folder2/Folder3").exists()
无论文件夹是否存在,它总是返回False 。谁能告诉我如何实现这一目标?
我知道 Blob 容器中不存在空文件夹,但我的意图是检查文件夹是否存在,然后继续其他业务逻辑。
现在,我正在库的帮助下在内存中创建一个 Parquet 文件,并将其上传到Azure Blob storage. 不过,这次我想要更简单的东西,即只是将一个文本文件写入DateTime.UtcNow内存中的文本文件并将其上传到 Azure。
到目前为止我的代码:
BlobServiceClient BlobServiceClient = new BlobServiceClient("my_code");
var containerClient = BlobServiceClient.GetBlobContainerClient("my_container");
var blobClient = containerClient.GetBlockBlobClient($"path/to/specific/directory/file.parquet");
using (var outStream = await blobClient.OpenWriteAsync(true).ConfigureAwait(false))
using (ChoParquetWriter parser = new ChoParquetWriter(outStream))
{
parser.Write(data_list);
}
Run Code Online (Sandbox Code Playgroud)
ChoParquetWriter如果没有我正在使用的库的一部分,如何在内存中创建文本文件?
更新:找到解决方案
using (var outStream = await blobClient4.OpenWriteAsync(true).ConfigureAwait(false))
{
byte[] bytes = null;
using (var ms = new MemoryStream())
{
TextWriter tw = new StreamWriter(ms);
tw.Write(RequestTime.ToString());
tw.Flush();
ms.Position = 0;
bytes = ms.ToArray();
ms.WriteTo(outStream);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法?如果不是,没关系
我需要附加到 Azure blob 文件的末尾,我想知道是否有一种方法可以做到这一点,而无需下载整个 blob 文件。我已经四处搜索,但似乎大多数答案都使用现已弃用的程序集。我弄清楚如何在 .NET Core 3.1 中执行此操作的唯一方法是使用下面的代码。有没有更有效的方法来做到这一点?我知道,根据我的要求,这个文件最终会变得非常大,并且下载整个文件只是为了在其末尾添加一个快速行可能会变得效率低下。
我正在使用程序集 Azure.Storage.Blobs
public static void BlobAppender()
{
BlobClient bc = new BlobClient("my azure connection string", "mycontainer", "myblobfile");
var testStream = bc.OpenRead();
StringBuilder SB = new StringBuilder();
using (StreamReader sm = new StreamReader(testStream))
{
string temp;
while ((temp = sm.ReadLine()) != null)
{
SB.AppendLine(temp);
}
}
//I append the line here, then reupload.
SB.AppendLine("My new line");
byte[] byteArray = Encoding.UTF8.GetBytes(SB.ToString());
MemoryStream stream = new MemoryStream(byteArray);
bc.Upload(stream, overwrite: true);
}
Run Code Online (Sandbox Code Playgroud) 我正在做一个 ASP .NET Core MVC Web 应用程序。Web 应用程序中有一个模块可以将文件上传到 blob 存储。
以下是我用来将文件上传到 blob 存储的函数:
public CloudBlockBlob UploadBlob(string BlobName, string ContainerName, IFormFile file)
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(ContainerName.ToLower());
CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);
try
{
blockBlob.UploadFromStreamAsync((Stream) file); //trying to convert FormFile to Stream type object and upload to blob storage
return blockBlob;
}
catch (Exception e)
{
var r = e.Message;
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
在那里,我已经将FormFile对象转换'file'为块中给出的Stream对象try。
但没有将文件上传到 blob 存储,而是给出异常:
Unable …
以下代码演示了将文件上传到 Azure BLOB。因此,我将上传较大的文件,因此存在由于低带宽问题而导致上传失败的风险(在SO中讨论)。
以下代码工作正常,但我需要一种方法以百分比 % 的形式向最终用户显示上传进度。我该如何修改我的代码以使其成为可能?
BlobUploadOptions op = new BlobUploadOptions
{
TransferOptions = new StorageTransferOptions
{
MaximumTransferSize = 4 * 1024 * 1024,
InitialTransferSize = 4 * 1024 * 1024,
MaximumConcurrency= 5
},
HttpHeaders = new BlobHttpHeaders
{
ContentType = request.Request.ContentType
}
};
Azure.Response<BlobContentInfo> ci = await
blobClient.UploadAsync(fileStream, op);
Run Code Online (Sandbox Code Playgroud) 我见过的大多数(全部?)Azure 存储 Python SDK 示例都演示了如何创建一个BlobServiceClient用于BlobClient上传/下载 blob(ref1、ref2等)的 。
为什么要创建一个BlobServiceClientthen aBlobClient而不是直接创建一个BlobClient?
例子:
from azure.storage.blob import BlobClient
def create_blob_client(connection_string):
try:
blob_client = BlobClient.from_connection_string(connection_string)
except Exception as e:
logging.error(f"Error creating Blob Service Client: {e}")
return blob_client
connection_string = os.environ["CONNECTION_STRING"]
blob_client = create_blob_client(connection_string)
Run Code Online (Sandbox Code Playgroud) 我尝试从天蓝色门户创建用户委托密钥。
无论我为自己分配什么权限,我都会遇到相同的错误消息
您无权授予读取访问权限。您仍然可以创建共享访问签名,但需要具有附加权限的 RBAC 角色,然后才能向签名收件人授予该级别的访问权限。了解有关用于访问 Blob 数据的 Azure 角色的详细信息
到目前为止,我分配了以下角色:
错误消息中提供的链接表示我需要以下其中一项:
所以它应该有效,但事实并非如此。我缺少什么?
我试图从内存流中的azure的blob存储中下载pdf文件.但是,当我尝试打开文件时,它不会加载.
这是我正在使用的代码. 下载代码
protected void blobDownload_Click(object sender, EventArgs e)
{
//Getting the blob storage container values
DBAccess dbaCon = new DBAccess();
DataTable dt = dbaCon.GetTrainingRecord(TrainingTrainingRecordID.Value);
string companyID = dt.Rows[0].Field<string>("fk_company_id");
string blobStorageName = dt.Rows[0].Field<string>("uploaded_file");
string filename = dt.Rows[0].Field<string>("display_file_name");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(System.Text.RegularExpressions.Regex.Replace(companyID.ToLower(), @"\s+", ""));
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobStorageName);
MemoryStream memStream = new MemoryStream();
blockBlob.DownloadToStream(memStream);
blockBlob.FetchAttributes();
HttpResponse response = HttpContext.Current.Response;
//response.ContentType = blockBlob.Properties.ContentType;
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "Attachment; filename=" + filename);
response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
response.BinaryWrite(memStream.ToArray()); …Run Code Online (Sandbox Code Playgroud) 我是azure的新手,正在为我的应用程序使用一个存储帐户。基本上我在azure blob存储中存储了json文件。
我想从Node JS应用程序中的这些文件中读取数据,并对数据进行一些过滤,最终将其作为受保护的REST端点,以将UI / Client中的数据作为HTTP响应进行查看。
我浏览了有关Blob存储上不同操作的文档,该文档公开为NODE SDK,我们可以在下面的链接中找到它们,
https://github.com/Azure/azure-storage-node
但是我有一个问题是“如何读取json文件”。我看到一种方法getBlobToStream。这是否会在回调中为我提供json内容,以便我可以对数据做进一步处理并将其作为响应发送给请求的客户端。
请有人解释如何更好地执行此操作,或者这是我们唯一的选择。
谢谢您的帮助。
azure azure-storage-blobs node.js azure-node-sdk azure-blob-storage
根据我的经验,AWS S3具有一个称为预签名的功能。这使我可以发出具有特定到期时间的URL进行访问。
我无法在Azure官方网站上找到有关此内容的任何详细信息。
因此,有人知道Azure Blob存储是否可以支持预签名的url功能吗?