Google Cloud Storage不会更新现有的默认Content-Disposition,而是使用.NET客户端库创建新的Content-Disposition元数据

问题描述 投票:0回答:1

enter image description here在为Content-Disposition属性创建/更新Google云存储对象元数据时,它正在添加新属性,而不是更新现有的Content-Disposition。请参阅下图。

我的目标是在下载对象时提供不同的名称。当我手动更新Content-Disposition时,它按预期工作。

我使用的是.NET客户端库,下面是代码

string fileNameWithExt = "filename.txt";

            using (var stream = file.InputStream)
            {
                var obj = new Google.Apis.Storage.v1.Data.Object
                {
                    Bucket = bucketName,
                    Name = fileName,
                    ContentType = "application/octet-stream",
                    Metadata = new Dictionary<string, string>
                        {
                            { "Content-Disposition", $"attachment; filename={fileNameWithExt}" }
                        }
                };

                var gcsObject = storage.UploadObject(obj, stream);

                var patchObject = new Google.Apis.Storage.v1.Data.Object
                {
                    Bucket = bucketName,
                    Name = fileName,
                    //ContentType = "text/plain",
                    Metadata = new Dictionary<string, string>
                    {
                        { "Content-Disposition", $"attachment; filename={fileNameWithExt}" }
                    }
                };
                storage.PatchObject(patchObject);
c# .net google-cloud-storage metadata
1个回答
3
投票

GCS对象具有各种属性,包括其名称,内容类型以及内容配置。但是,它们还具有另一个属性:任意用户元数据。这是一个键值对的列表,可以包含您喜欢的任何内容。

C#库调用自定义用户元数据键值字典Metadata。通过使用该属性,您的代码将使用“Content-Disposition”键创建自定义用户元数据条目。相反,使用ContentDisposition值。像这样的东西:

var patchObject = new Google.Apis.Storage.v1.Data.Object
{
    Bucket = bucketName,
    Name = fileName,
    ContentDisposition = $"attachment; filename={fileNameWithExt}" 
}

此外,如果您正在编写新的C#代码,我建议使用更新且更易于使用的google-cloud .NET库:https://googlecloudplatform.github.io/google-cloud-dotnet/

© www.soinside.com 2019 - 2024. All rights reserved.