Openstackdotnet SDK对象存储对象版本控制
本文关键字:对象 版本控制 存储 Openstackdotnet SDK | 更新日期: 2023-09-27 18:25:24
我正在使用OpenStack SDK开发云备份客户端。NET。在我之前的问题中,我在身份识别方面遇到了问题,它得到了完美的解决。现在,我测试了几乎所有我需要的功能,但有一点似乎不正常。我需要在对象存储swift中创建对象版本控制功能。我在Openstack的官方文档中读到,我需要在Rest:中添加一个标题
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:{"意外的HTTP错误:PreconditionFailed"}:
public void UploadFile(string _containerName, string _fileName, ThrottledStream _stream)
{
filesProvider.CreateObject(_containerName, _stream, _fileName, null, 4096, null, "RegionOne");
}
这是创建启用版本控制的容器的正确方法吗?
基于该错误,您在创建对象时是否发送了"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);
}
}
}