Skip to content

Commit

Permalink
Merge pull request #41 from aliyun/restorebuket
Browse files Browse the repository at this point in the history
 Supports IA/Archive in CreateBucket API and Restore API
  • Loading branch information
baiyubin2020 authored Dec 1, 2017
2 parents 3e4cd25 + f241587 commit b7550cf
Show file tree
Hide file tree
Showing 28 changed files with 486 additions and 37 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: csharp
solution: aliyun-oss-sdk.sln
mono:
- latest
- 4.2.1
- 4.0.0
- 5.4.1
- 4.8.1
- 3.12.0
env:
global:
- MONO_IOMAP=all
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ChangeLog - Aliyun OSS SDK for C#

## 版本号:2.6.0 日期:2017/11/30
### 变更内容
- 增加:CreateBucket支持StorageClass
- 增加:支持RestoreObject

## 版本号:2.5.4 日期:2017/11/17
### 变更内容
- 修复:多线程调用BeginPutObject报ObjectDisposedException的问题
Expand Down
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- OSS C# SDK[在线文档](http://gosspublic.alicdn.com/AliyunNetSDK/apidocs/latest/index.html)

## 版本
- 当前版本:2.5.4
- 当前版本:2.6.0

## 运行环境

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- OSS C# SDK[Online Documentation](http://gosspublic.alicdn.com/AliyunNetSDK/international/apidocs/latest/index.html).

## Version
- Current version: 2.5.4.
- Current version: 2.6.0.

## Run environment

Expand Down
4 changes: 2 additions & 2 deletions samples/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("2.5.4")]
[assembly: AssemblyVersion("2.6.0")]
[assembly: NeutralResourcesLanguage("zh-CN")]
[assembly: AssemblyFileVersion("2.5.4")]
[assembly: AssemblyFileVersion("2.6.0")]

53 changes: 53 additions & 0 deletions samples/Samples/RestoreArchiveObjectSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/

using System;
using System.Net;
using System.Threading;
using Aliyun.OSS.Model;
using Aliyun.OSS.Common;

namespace Aliyun.OSS.Samples
{
/// <summary>
/// Sample usage of RestoreObject API
/// </summary>
public static class RestoreArchiveObjectSample
{
static string accessKeyId = Config.AccessKeyId;
static string accessKeySecret = Config.AccessKeySecret;
static string endpoint = Config.Endpoint;
static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);

public static void RestoreArchiveObject(string bucketName, string key, bool waitUtilFinished = true, int maxWaitTimeInSeconds = 600)
{
RestoreObjectResult result = client.RestoreObject(bucketName, key);
if (result.HttpStatusCode != HttpStatusCode.Accepted || !waitUtilFinished)
{
throw new OssException(result.RequestId + ", " + result.HttpStatusCode + " ,");
}

while (maxWaitTimeInSeconds > 0)
{
var meta = client.GetObjectMetadata(bucketName, key);
string restoreStatus = meta.HttpMetadata["x-oss-restore"] as string;
if (restoreStatus != null && restoreStatus.StartsWith("ongoing-request=\"false\"", StringComparison.InvariantCultureIgnoreCase))
{
break;
}

Thread.Sleep(1000);
maxWaitTimeInSeconds--;
}

if (maxWaitTimeInSeconds == 0)
{
throw new TimeoutException();
}
}

}
}
2 changes: 1 addition & 1 deletion samples/aliyun-oss-sdk-samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</TargetFrameworkProfile>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SignAssembly>False</SignAssembly>
<DelaySign>False</DelaySign>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
Expand Down Expand Up @@ -125,6 +124,7 @@
<Compile Include="Samples\SetBucketAclSample.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Samples\RestoreArchiveObjectSample.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down
24 changes: 21 additions & 3 deletions sdk/Commands/CreateBucketCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/

using System;
using System.IO;
using Aliyun.OSS.Common.Communication;
using Aliyun.OSS.Transform;
using Aliyun.OSS.Util;

namespace Aliyun.OSS.Commands
Expand All @@ -24,19 +26,35 @@ protected override string Bucket
get { return _bucketName; }
}

protected override Stream Content
{
get
{
return StorageClass == null ? null : SerializerFactory.GetFactory().CreateCreateBucketSerialization()
.Serialize(StorageClass.Value);
}
}

protected StorageClass? StorageClass
{
get;
set;
}

private CreateBucketCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
string bucketName)
string bucketName, StorageClass? storageClass)
: base(client, endpoint, context)
{
OssUtils.CheckBucketName(bucketName);
_bucketName = bucketName;
StorageClass = storageClass;
}

public static CreateBucketCommand Create(IServiceClient client, Uri endpoint,
ExecutionContext context,
string bucketName)
string bucketName, StorageClass? storageClass = null)
{
return new CreateBucketCommand(client, endpoint, context, bucketName);
return new CreateBucketCommand(client, endpoint, context, bucketName, storageClass);
}
}
}
9 changes: 8 additions & 1 deletion sdk/Commands/OssCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ protected bool UseChunkedEncoding
private set;
}

protected bool ParametersInUri
{
get;
set;
}

protected OssCommand(IServiceClient client, Uri endpoint, ExecutionContext context)
: this(client, endpoint, context, false)
{
Expand Down Expand Up @@ -104,7 +110,8 @@ private ServiceRequest BuildRequest()
Method = Method,
Endpoint = OssUtils.MakeBucketEndpoint(Endpoint, Bucket, conf),
ResourcePath = OssUtils.MakeResourcePath(Endpoint, Bucket, Key),
UseChunkedEncoding = UseChunkedEncoding
UseChunkedEncoding = UseChunkedEncoding,
ParametersInUri = ParametersInUri
};

foreach (var p in Parameters)
Expand Down
70 changes: 70 additions & 0 deletions sdk/Commands/RestoreObjectCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/

using System;
using System.Collections.Generic;
using Aliyun.OSS.Common.Communication;
using Aliyun.OSS.Util;
using Aliyun.OSS.Transform;
using Aliyun.OSS.Properties;
using Aliyun.OSS.Model;

namespace Aliyun.OSS.Commands
{
internal class RestoreObjectCommand : OssCommand<RestoreObjectResult>
{
private string bucketName;
private string key;

protected override string Bucket
{
get {
return bucketName;
}
}

protected override string Key
{
get {
return key;
}
}

protected override HttpMethod Method
{
get { return HttpMethod.Post; }
}

protected override IDictionary<string, string> Parameters
{
get
{
var parameters = new Dictionary<string, string>();
parameters[RequestParameters.SUBRESOURCE_RESTORE] = null;
return parameters;
}
}

private RestoreObjectCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
string bucketName, string key, IDeserializer<ServiceResponse, RestoreObjectResult> deserializer)
: base(client, endpoint, context, deserializer)
{
OssUtils.CheckBucketName(bucketName);
OssUtils.CheckObjectKey(key);

this.bucketName = bucketName;
this.key = key;
this.ParametersInUri = true; // in restore request, the parameter restore needs to be in uri
}

public static RestoreObjectCommand Create(IServiceClient client, Uri endpoint,
ExecutionContext context,
string bucketName, string key)
{
return new RestoreObjectCommand(client, endpoint, context, bucketName, key, DeserializerFactory.GetFactory().CreateRestoreObjectResultDeserializer());
}
}
}
9 changes: 9 additions & 0 deletions sdk/Common/Communication/ServiceClientImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,16 @@ private static void SetRequestHeaders(HttpWebRequest webRequest, ServiceRequest
// with the WebHeaderCollection.Add method,
// we have to call an internal method to skip validation.
foreach (var h in serviceRequest.Headers)
{
// Nginx does not accept a chunked encoding request with Content-Length, as detailed in #OSS-2848
if (h.Key.Equals(HttpHeaders.ContentLength) && (serviceRequest.UseChunkedEncoding ||
(serviceRequest.Content != null && !serviceRequest.Content.CanSeek)))
{
continue;
}

HttpExtensions.AddInternal(webRequest.Headers, h.Key, h.Value);
}

// Set user-agent
if (!string.IsNullOrEmpty(configuration.UserAgent))
Expand Down
13 changes: 12 additions & 1 deletion sdk/Common/Communication/ServiceRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public bool UseChunkedEncoding
set;
}

/// <summary>
/// Gets or sets a value indicating whether this <see cref="T:Aliyun.OSS.Common.Communication.ServiceRequest"/>
/// parameters in URL.
/// </summary>
/// <value><c>true</c> if parameters in URL; otherwise, <c>false</c>.</value>
public bool ParametersInUri
{
get;
set;
}

/// <summary>
/// Build the request URI from the request message.
/// </summary>
Expand Down Expand Up @@ -112,7 +123,7 @@ private bool IsParameterInUri()
{
var requestHasPayload = Content != null;
var requestIsPost = Method == HttpMethod.Post;
var putParamsInUri = !requestIsPost || requestHasPayload;
var putParamsInUri = !requestIsPost || requestHasPayload || ParametersInUri;
return putParamsInUri;
}

Expand Down
18 changes: 18 additions & 0 deletions sdk/Enums/StorageClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/
using System;
namespace Aliyun.OSS
{
/// <summary>
/// Storage class of OSS Bucket
/// </summary>
public enum StorageClass
{
Standard, // Standard bucket
IA, // Infrequent Access bucket
Archive // Archive bucket
}
}
17 changes: 17 additions & 0 deletions sdk/IOss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using Aliyun.OSS.Common.Authentication;
using Aliyun.OSS.Common.Internal;
using Aliyun.OSS.Model;

namespace Aliyun.OSS
{
Expand Down Expand Up @@ -53,6 +54,14 @@ public interface IOss
/// <returns><see cref="Bucket" /> instance</returns>
Bucket CreateBucket(string bucketName);

/// <summary>
/// Creates the bucket with specified storage class.
/// </summary>
/// <returns>The bucket.</returns>
/// <param name="bucketName">Bucket name.</param>
/// <param name="storageClass">Storage class.</param>
Bucket CreateBucket(string bucketName, StorageClass? storageClass);

/// <summary>
/// Deletes a empty bucket.If the bucket is not empty, this will fail.
/// </summary>
Expand Down Expand Up @@ -656,6 +665,14 @@ PutObjectResult ResumableUploadObject(string bucketName, string key, Stream cont
/// <returns><see cref="AccessControlList" /> instance</returns>
AccessControlList GetObjectAcl(string bucketName, string key);

/// <summary>
/// Restores the object.
/// </summary>
/// <returns>The object.</returns>
/// <param name="bucketName">Bucket name.</param>
/// <param name="key">Key.</param>
RestoreObjectResult RestoreObject(string bucketName, string key);

#endregion

#region Generate URL
Expand Down
18 changes: 18 additions & 0 deletions sdk/Model/CreateBucketRequestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/

using System;
using System.Xml.Serialization;

namespace Aliyun.OSS.Model
{
[XmlRoot("CreateBucketConfiguration")]
public class CreateBucketRequestModel
{
[XmlElement("StorageClass")]
public StorageClass StorageClass { get; set; }
}
}
15 changes: 15 additions & 0 deletions sdk/Model/RestoreObjectResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/

using System;
using System.Xml.Serialization;

namespace Aliyun.OSS.Model
{
public class RestoreObjectResult : GenericResult
{
}
}
Loading

0 comments on commit b7550cf

Please sign in to comment.