简体   繁体   中英

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.

My Goal is to provide a different name when downloading the object.When I manually update the Content-Disposition it is working as expected.

I am using .NET client libraries and below is the code

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. 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 . By using that property, your code is creating a custom user metadata entry with a key of "Content-Disposition". Instead, use the ContentDisposition value. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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