2015-12-03 60 views
1

我正在使用OpenStack SDK .NET在云备份客户端上工作。在我的previous question中,我遇到了识别问题,并且解决了问题。现在我测试了我需要的几乎所有功能,但有一件事似乎并不正确。 我需要在对象存储swift中创建对象版本控制的功能。我正式OpenStack的文件,我需要一个头添加到安息阅读:Openstackdotnet SDK对象存储对象版本

X-Versions-Location: myversionlocation 

在我的图书馆我修改我的方法从创建容器:

public void CreateContainer(string _containerName) 
{ 
    filesProvider.CreateContainer(_containerName, null, null, false, null); 
} 

到:

public void CreateContainer(string _containerName) 
{ 
    Dictionary<string, string> versioningHeader = new Dictionary<string, string>(); 
    versioningHeader.Add("X-Versions-Location", "versions"); 
    filesProvider.CreateContainer(_containerName, versioningHeader, null, false, null); 
} 

当我在容器中上传文件时没有问题,但是当我第二次上载文件时,我的应用程序会在此行中引发ResponseException:{“Unexpected HTTP error:PreconditionFailed”}:

public void UploadFile(string _containerName, string _fileName, ThrottledStream _stream) 
{ 
    filesProvider.CreateObject(_containerName, _stream, _fileName, null, 4096, null, "RegionOne"); 
} 

这是创建具有启用版本控制的容器的正确方法吗?

回答

0

基于这个错误,你可能在创建对象时发送“if-none-match”头文件吗?如果设置了该标题,并且您尝试上载容器中已存在的文件,则会引发该错误。我不相信你可以在版本中使用这个头文件,因为这个检查是基于名字而不是上传文件的散列。只是一个猜测。 :-)

下面是一个控制台应用程序,它在上传新版本之前上传文件两次,以证明这应该起作用。

using System; 
using System.Collections.Generic; 
using net.openstack.Core.Domain; 
using net.openstack.Core.Providers; 
using net.openstack.Providers.Rackspace; 

namespace CloudFilesVersioning 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var identityUrl = new Uri("{identity-url}"); 
      var identity = new CloudIdentityWithProject 
      { 
       Username = "{user}", 
       ProjectName = "{project-name}", 
       Password = "{password}" 
      }; 
      const string region = "RegionOne"; 

      var identityProvider = new OpenStackIdentityProvider(identityUrl, identity); 
      var filesProvider = new CloudFilesProvider(null, identityProvider); 

      // Create versions container 
      const string versionContainerName = "mycontainer-versions"; 
      filesProvider.CreateContainer(versionContainerName, region: region); 

      // Create main container 
      const string containerName = "mycontainer"; 
      var headers = new Dictionary<string, string> 
      { 
       {"X-Versions-Location", versionContainerName} 
      }; 
      filesProvider.CreateContainer(containerName, headers, region); 

      // Upload the initial file 
      filesProvider.CreateObjectFromFile(containerName, @"C:\thing-v1.txt", "thing.txt", region: region); 

      // Upload the same file again, this should not create a new version 
      filesProvider.CreateObjectFromFile(containerName, @"C:\thing-v1.txt", "thing.txt", region: region); 

      // Upload a new version of the file 
      filesProvider.CreateObjectFromFile(containerName, @"C:\thing-v2.txt", "thing.txt", region: region); 
     } 
    } 
}