简体   繁体   English

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

[英]Google Cloud Storage not updating the existing default Content-Disposition but creating a new Content-Disposition metadata with .NET Client Libraries

在此处输入图片说明 When creating/updating google cloud storage object metadata for Content-Disposition property, it is adding a new property instead of updating the existing Content-Disposition.Please see the image below. 为Content-Disposition属性创建/更新Google云存储对象元数据时,它是在添加新属性而不是更新现有的Content-Disposition。请参见下图。

My Goal is to provide a different name when downloading the object.When I manually update the Content-Disposition it is working as expected. 我的目标是在下载对象时提供一个不同的名称。当我手动更新Content-Disposition时,它可以正常工作。

I am using .NET client libraries and below is the code 我正在使用.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);

GCS objects have a variety of properties, including their name, their content type, and, as you noted, the content disposition. GCS对象具有各种属性,包括它们的名称,内容类型以及内容处置,如您所指出的。 However, they also have another property: arbitrary user metadata. 但是,它们还具有另一个属性:任意用户元数据。 This is a list of key-value pairs of strings which can contain whatever you like. 这是键值对字符串的列表,其中可以包含您喜欢的任何内容。

The C# library calls the custom user metadata key-value dictionary Metadata . C#库调用自定义用户元数据键值字典Metadata By using that property, your code is creating a custom user metadata entry with a key of "Content-Disposition". 通过使用该属性,您的代码将创建一个键为“ Content-Disposition”的自定义用户元数据条目。 Instead, use the ContentDisposition value. 而是使用ContentDisposition值。 Something like this: 像这样:

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

Also, if you're writing new C# code, I recommend the newer and easier to use google-cloud .NET library: https://googlecloudplatform.github.io/google-cloud-dotnet/ 另外,如果您正在编写新的C#代码,我建议使用更新且更易于使用的google-cloud .NET库: https : //googlecloudplatform.github.io/google-cloud-dotnet/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM