Project Description (项目介绍)
Baidu BCS (Baidu Cloud Storage) Server SDK for .NET
百度云存储服务器SDK (.NET平台)
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 });