Project Description (项目介绍)
Baidu BCS (Baidu Cloud Storage) Server SDK for .NET
百度云存储服务器SDK (.NET平台)

 

Source code language (源代码语言): C# 
.NET 4.0 or above (.NET 4.0或更高版本)

Latest Version (最新版本): 1.0.0 

Baidu Official documentation (百度官方文档): http://developer.baidu.com/wiki/index.php?title=docs/cplat/stor/api

 

Classical Usage Sample (经典使用示例)

The following code would show you how to create, get and delete bucket in BaiduBCSClient

(以下代码会示范如何使用BaiduBCSClient创建,获取和删除bucket)

// You need to assign your bucket in your code
string bucket = "TestBucket";
// You need to assign your app key and your secret key in your code
var client = new BaiduBCSClient("your app key", "your secret key");

// According to bucket naming rule by Baidu, all bucket names would be changed to lower case inside client for any bucket operation.
client.CreateBucket(bucket);

// Get bucket list and output summary via Console.
var bucketList = client.GetBucketList();

foreach (var one in bucketList)
{
    Console.WriteLine(string.Format("{Bucket: {0}, TotalCapacity: {1}.", one.Name, one.TotalCapacity));
}

// Delete bucket.
client.DeleteBucket(bucket);

The following code would show you how to create, get and delete object in BaiduBCSClient

(以下代码会示范如何使用BaiduBCSClient创建,获取和删除object)

// You need to assign your bucket in your code
string bucket = "TestBucket";
// You need to assign your object name (file name) in your code
string objectName = "test.txt";
// You need to assign your app key and your secret key in your code
var client = new BaiduBCSClient("your app key", "your secret key");

// Firstly, we need to create a file for upload test later.
//Supposed that the test machine has disk D and application has enough permission to operate on that.
File.WriteAllText(@"D:\" + objectName, "Test string \n\r Hello world.");

// Read/Load the bytes for creating object.
byte[] content = File.ReadAllBytes(@"D:\" + objectName);

// Upload bytes and file information
var md5 = client.CreateStorageObject(new StorageObjectUploadRequest
      {
          Bytes = content,
          BucketName = bucket,
          ObjectName = objectName,
          DownloadFileName = "DownloadName.dat",
          ContentType = "text/plain",
          IsSuperFile = false,
          Permission = StorageObjectPermission.PublicReadAndWrite
      });

// We try to get meta data for the uploaded file.
var meta = client.GetStorageObjectMetaInfo(new StorageObjectDownloadRequest
{
    BucketName = bucket,
    ObjectName = objectName
});

// We try to get whole byte data for the uploaded file.
var data = client.GetStorageObject(new StorageObjectDownloadRequest
{
    BucketName = bucket,
    ObjectName = objectName
});

// We also can save file in local by specific path
client.SaveStorageObjectAs(new StorageObjectDownloadRequest
{
    BucketName = bucket,
    ObjectName = objectName
}, @"D:\DownloadedTest.txt");

// Test file needs to be deleted.
client.DeleteStorageObject(new StorageObjectIdentity
{
    BucketName = bucket,
    ObjectName = objectName
});

 

Please refer Documentation tab for more information, or download CHM documentation.

(更多信息详见 Documentation 栏,或者下载CHM格式文档)

For any question or concern, please contact me.

(有任何疑问或问题,请联系 作者)

Last edited Aug 13, 2014 at 8:00 AM by rynnwang, version 6