进入正题前先说一下我遇到的问题:
①: 这是因为本地时间与服务器时差太大的原因 --你可检查一下你的本地时区,(阿里的程序猿竟突让我换个接口试试!)
②: 我在从OSS上获取图片时因为endpoint缺少一个参数而产生的,
误:http://oss-cn-beijing.aliyuncs.com/developer/2/2.jpg
正:http://oss-cn-beijing.aliyuncs.com/gevek/developer/2/2.jpg
※ ※ ※ ※ ※ ※ coding前首先介绍一下OSS三个基本要素/ bucket AccessKey AccessID Services
1、Bucket :
Bucket是OSS上的命名空间,也是计费、权限控制、日志记录等高级功能的管理实体;Bucket名称在整个OSS服务中具有全局唯一性,且不能修改;存储在OSS上的每个Object必须都包含在某个Bucket中。一个应用,例如图片分享网站,可以对应一个或多个Bucket。一个用户最多可创建10个Bucket,但每个Bucket中存放的Object的数量和大小总和没有限制,用户不需要考虑数据的可扩展性。
2、AccessKey AccessID :
用户注册OSS时,系统会给用户分配一对Access Key ID和Access Key Secret,称为ID对,用于标识用户,为访问OSS做签名验证。
3、service
OSS提供给用户的虚拟存储空间,在这个虚拟空间中,每个用户可拥有一个到多个Bucket。
在此 给需要的童鞋提供一下 SDK下载链接 http://pan.baidu.com/s/1kTm3sDx;
下面进入正题:具体展示三个基本要素的基本使用。一、单文件上传
const string _accessId = "your access ID";
const string _accessKey = " your access Key";
const string _endpoint = "http://oss-cn-(服务器位置).aliyuncs.com";
private static readonly OssClient _ossClient = new OssClient(_endpoint, _accessId, _accessKey);
public bool PutSingleObject(String bucketName, String objectName, String fileToUpload)
{
bool resul = false;
try
{
OssClient client = new OssClient(_endpoint, _accessId, _accessKey);
using (var fs = File.Open(fileToUpload, FileMode.Open))
{
var metadata = new ObjectMetadata();
metadata.UserMetadata.Add("mykey1", "myval1");
metadata.CacheControl = "No-Cache";
metadata.ContentLength = fs.Length;
client.PutObject(bucketName, objectName, fs, metadata);
MessageBox.Show(client.ToString());
}
resul=true;
}
catch (OssException )
{
MessageBox.Show("请检查网络");
resul = false;
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
// ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception )
{
MessageBox.Show("请检查网络");
resul = false;
// Console.WriteLine("Failed with error info: {0}", ex.Message);
}
return resul;
}
二 、多文件上传 (带进度条) UploadMultipart
public class UploadThread
{
public delegate void ShowProgressDelegate(int totalStep, int currentStep);
private ShowProgressDelegate process_;
private Control invokeControl_;
private long totalSize;
private long currentSize;
private long currentsizenum;
public long TotalSize
{
get { return totalSize; }
}
public long CurrentSize
{
get { return this.currentsizenum; }
}
public float CurrentProgress
{
get
{
if (this.totalSize != 0)
{
return (float)this.currentsizenum * 100 / (float)this.totalSize;
}
else
{
return 0;
}
}
}
public string UploadMultipart(String bucketName, String objectName, String fileToUpload, int partSize, ShowProgressDelegate prosess, Control t)
{
invokeControl_ = t;
process_ = prosess;
var uploadId = InitiateMultipartUpload(bucketName, objectName);
var partETags = UploadParts(bucketName, objectName, fileToUpload, uploadId, partSize);
var completeResult = CompleteUploadPart(bucketName, objectName, uploadId, partETags);
return completeResult.BucketName;
}
private static string InitiateMultipartUpload(String bucketName, String objectName)
{
var request = new InitiateMultipartUploadRequest(bucketName, objectName);
var result = _ossClient.InitiateMultipartUpload(request);
return result.UploadId;
}
private List<PartETag> UploadParts(String bucketName, String objectName, String fileToUpload, String uploadId, int partSize)
{
var fi = new FileInfo(fileToUpload);
var fileSize = fi.Length;
totalSize = fileSize;
var partCount = fileSize / partSize;
if (fileSize % partSize != 0)
{
partCount++;
}
long size = 0;
var partETags = new List<PartETag>();
for (var i = 0; i < partCount; i++)
{
using (var fs = File.Open(fileToUpload, FileMode.Open))
{
var skipBytes = (long)partSize * i;
fs.Seek(skipBytes, 0);
size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
var request = new UploadPartRequest(bucketName, objectName, uploadId)
{
InputStream = fs,
PartSize = size,
PartNumber = i + 1
};
var result = _ossClient.UploadPart(request);
partETags.Add(result.PartETag);
currentSize = size;
}
currentsizenum += currentSize;
object[] objs = new object[] { 100, 0 };
objs[1] = (int)CurrentProgress;
invokeControl_.Invoke(process_, objs);
}
return partETags;
}
private CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName,
String uploadId, List<PartETag> partETags)
{
var completeMultipartUploadRequest =
new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);
foreach (var partETag in partETags)
{
completeMultipartUploadRequest.PartETags.Add(partETag);
}
return _ossClient.CompleteMultipartUpload(completeMultipartUploadRequest);
}
}
第一次写 还望各位大虾 多多指教!感谢洋哥的指导