簡體   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

在此處輸入圖片說明 為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);

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM