简体   繁体   English

软层对象存储ETag MD5校验和计算

[英]Softlayer Object Storage ETag MD5 Checksum Calculation

I'm trying to figure out how to calculate the correct checksum when passing data to the Softlayer Object Storage. 我试图弄清楚将数据传递到Softlayer对象存储时如何计算正确的校验和。

I know the ETag is the issue because if I remove it form the request it works, however I'd prefer to use it to verify the uploads are not corrupt. 我知道ETag是问题所在,因为如果我从请求中将其删除,则它可以正常工作,但是我更喜欢使用它来验证上传内容是否未损坏。

This is my method: 这是我的方法:

    public bool SaveFile(byte[] file, eFetchStorageContainers container, string internalFileName, string fileName = "", bool overPublicNetwork = false)
    {
        Authenticate(overPublicNetwork);

        client = new RestClient(storage_url);
        var resourcePath = string.Format("/{0}/{1}", container, internalFileName);
        var req = new RestRequest(resourcePath, RestSharp.Method.PUT);

        req.AddHeader("X-Auth-Token", auth_token);
        req.AddFile(internalFileName, file, fileName);

        var md5Checksum = BitConverter.ToString(MD5.Create().ComputeHash(file)).Replace("-", string.Empty).ToLower();
        req.AddHeader("ETag", md5Checksum);

        var resp = client.Execute(req);

        return false;
    } 

Here is how the ETag is defined: 以下是ETag的定义方式:

在此处输入图片说明

I believe the problem lies in the fact that i'm getting the checksum for the file and not the request body. 我认为问题在于我正在获取文件的校验和而不是请求正文。

  1. I want to verify that I should be getting the checksum of the Request Body and NOT the file alone. 我想验证我是否应该获取请求正文的校验和,而不是单独获取文件。

  2. If the above is true I'm not even sure how to get the checksum for the body - would love some guidance... 如果以上情况是正确的,我什至不知道如何获取身体的校验和-希望得到一些指导...

Well I did not use C#, but it works using curl fine for me. 好吧,我没有使用C#,但是对我来说,使用curl可以正常工作。 I get the checksum for the file and it is working fine. 我得到了文件的校验和,并且工作正常。 just in case here some examples about this https://community.runabove.com/kb/en/object-storage/how-to-check-file-consistency-using-etag-and-md5.html 以防万一,这里有一些关于这个https://community.runabove.com/kb/en/object-storage/how-to-check-file-consistency-using-etag-and-md5.html的示例

Make sure that your request is similar to examples of the link above. 确保您的请求与上面的链接示例相似。

This is the curl I used: 这是我使用的卷曲:

curl -X PUT -T "C:\\Users\\ncabero\\Downloads\\picture.jpg" -H "X-Auth-Token: AUTH_XXXXXXX" -H "Etag: a43bf68dd35599a7873c12128f71b1f4" https://dal05.objectstorage.softlayer.net/v1/AUTH_d684780d-aafe-4772-bcbb-0f07d5f6edf3/rcvtest/picture.jpg curl -X PUT -T“ C:\\ Users \\ ncabero \\ Downloads \\ picture.jpg” -H“ X-Auth-Token:AUTH_XXXXXXX” -H“ Etag:a43bf68dd35599a7873c12128f71b1f4” https://dal05.objectstorage.softlayer.net/ v1 / AUTH_d684780d-aafe-4772-bcbb-0f07d5f6edf3 / rcvtest / picture.jpg

I actually figured this out, I was using RestSharp however its impossible to get the request body. 我实际上已经弄清楚了,我在使用RestSharp,但是不可能获得请求正文。

I moved over to HttpClient and was able to access the request body to create a checksum. 我移到HttpClient并能够访问请求主体以创建校验和。

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-Auth-Token", auth_token);
var bytes = new ByteArrayContent(file);
var formData = new MultipartFormDataContent();
formData.Add(bytes, internalFileName, internalFileName);

// this creates a checksum to send over for verification of non corrupted transfers
// this is also prevents us from using RestSharp due to its inability to create a checksum of the request body prior to sending
var md5Checksum = BitConverter.ToString(MD5.Create().ComputeHash(formData.ReadAsByteArrayAsync().Result)).Replace("-", string.Empty).ToLower();
httpClient.DefaultRequestHeaders.Add("ETag", md5Checksum);

var url = string.Format("{0}/{1}{2}/{3}", storage_url, containerName, folderId, internalFileName);
var resp = httpClient.PutAsync(url, formData).Result;

httpClient.Dispose();

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

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