从官网下载aws 的unity插件,,可进行数据上传、下载等操作,文末代码在原代码基础上做了简单修改(主要用修改PostObject)。操作过程中主要存在以下几个问题:
1.获取Pool ID
其他参数比较容易获取,Pool ID则有有点隐藏:通过服务-Cognito-管理/新建用户池,可以新建或者获取Pool ID
2.上传失败
使用unity插件中S3Example中PostObject时抛异常,但是获取GetObject没问题,此时需要在上传时代码中加一下区域,如下图所示。
3.网络代理问题
如果返回网络代理问题,请查看是否正在科学上网,大部分科学上网都是代理服务器,所以Amazon返回错误数据。正常网络即可获取数据,虽然会慢一些。
4.代码
using UnityEngine;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.Runtime;
using System.IO;
using System;
using System.Collections.Generic;
using Amazon.CognitoIdentity;
using Amazon;
public class AmazonS3Sdk : MonoBehaviour
{
public string IdentityPoolId = "";
public string CognitoIdentityRegion = RegionEndpoint.APSoutheast1.SystemName;
private RegionEndpoint _CognitoIdentityRegion
{
get { return RegionEndpoint.GetBySystemName(CognitoIdentityRegion); }
}
public string S3Region = RegionEndpoint.APSoutheast1.SystemName;
private RegionEndpoint _S3Region
{
get { return RegionEndpoint.GetBySystemName(S3Region); }
}
public string S3BucketName = null;
public string SampleFileName = null;
void Start()
{
UnityInitializer.AttachToGameObject(this.gameObject);
AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
}
#region private members
private IAmazonS3 _s3Client;
private AWSCredentials _credentials;
private AWSCredentials Credentials
{
get
{
if (_credentials == null)
_credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion);
return _credentials;
}
}
private IAmazonS3 Client
{
get
{
if (_s3Client == null)
{
_s3Client = new AmazonS3Client(Credentials, _S3Region);
}
//test comment
return _s3Client;
}
}
#endregion
#region Get Bucket List
/// <summary>
/// Example method to Demostrate GetBucketList
/// </summary>
public void GetBucketList()
{
Debug.Log("Fetching all the Buckets");
Client.ListBucketsAsync(new ListBucketsRequest(), (responseObject) =>
{
Debug.Log(responseObject.Exception.ToString());
if (responseObject.Exception == null)
{
Debug.Log("Got Response");
responseObject.Response.Buckets.ForEach((s3b) =>
{
string info = string.Format("bucket = {0}, created date = {1} \n", s3b.BucketName, s3b.CreationDate);
Debug.Log(info);
});
}
else
{
//ResultText.text += "Got Exception " + responseObject.Exception.ToString();
Debug.Log("Fetching Buckets Exception:" + responseObject.Exception.ToString());
}
});
}
#endregion
/// <summary>
/// Get Object from S3 Bucket
/// </summary>
private void GetObject()
{
string info = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
Debug.Log(info);
Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
{
string data = null;
var response = responseObj.Response;
if (response.ResponseStream != null)
{
using (StreamReader reader = new StreamReader(response.ResponseStream))
{
data = reader.ReadToEnd();
}
Debug.Log(data);
//ResultText.text += data;
}
});
}
/// <summary>
/// Post Object to S3 Bucket.
/// </summary>
public void PostObject(string file,string key,Action<string> action)
{
Debug.Log("Posting the file");
var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
//ResultText.text += "\nCreating request object";
var request = new PostObjectRequest()
{
Bucket = S3BucketName,
//Key = fileName,
Key = key,
InputStream = stream,
CannedACL = S3CannedACL.Private,
Region = _S3Region
};
Debug.Log("Making HTTP post call");
Client.PostObjectAsync(request, (responseObj) =>
{
if (responseObj.Exception == null)
{
string info = string.Format("\nobject {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.Bucket);
Debug.Log(info);
if (action != null)
action(responseObj.Request.Key);
}
else
{
Debug.Log("Posting Exception:"+ responseObj.Exception.ToString());
//ResultText.text += string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString());
}
});
}
/// <summary>
/// Get Objects from S3 Bucket
/// </summary>
public void GetObjects()
{
Debug.Log("Fetching all the Objects from " + S3BucketName);
var request = new ListObjectsRequest()
{
BucketName = S3BucketName
};
Client.ListObjectsAsync(request, (responseObject) =>
{
//ResultText.text += "\n";
if (responseObject.Exception == null)
{
//ResultText.text += "Got Response \nPrinting now \n";
responseObject.Response.S3Objects.ForEach((o) =>
{
string info = string.Format("{0}\n", o.Key);
Debug.Log(info);
});
}
else
{
string info = "Fetching Objects Exception:"+ responseObject.Exception.ToString();
Debug.Log(info);
}
});
}
/// <summary>
/// Delete Objects in S3 Bucket
/// </summary>
public void DeleteObject()
{
string info = string.Format("deleting {0} from bucket {1}", SampleFileName, S3BucketName);
Debug.Log(info);
List<KeyVersion> objects = new List<KeyVersion>();
objects.Add(new KeyVersion()
{
Key = SampleFileName
});
var request = new DeleteObjectsRequest()
{
BucketName = S3BucketName,
Objects = objects
};
Client.DeleteObjectsAsync(request, (responseObj) =>
{
//ResultText.text += "\n";
if (responseObj.Exception == null)
{
//ResultText.text += "Got Response \n \n";
//ResultText.text += string.Format("deleted objects \n");
responseObj.Response.DeletedObjects.ForEach((dObj) =>
{
string str = dObj.Key;
Debug.Log(str);
});
}
else
{
string str = "Got Exception \n";
Debug.Log(str);
}
});
}
private string GetFileHelper()
{
var fileName = SampleFileName;
if (!File.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName))
{
var streamReader = File.CreateText(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName);
streamReader.WriteLine("This is a sample s3 file uploaded from unity s3 sample");
streamReader.Close();
}
return fileName;
}
private string GetPostPolicy(string bucketName, string key, string contentType)
{
bucketName = bucketName.Trim();
key = key.Trim();
// uploadFileName cannot start with /
if (!string.IsNullOrEmpty(key) && key[0] == '/')
{
throw new ArgumentException("uploadFileName cannot start with / ");
}
contentType = contentType.Trim();
if (string.IsNullOrEmpty(bucketName))
{
throw new ArgumentException("bucketName cannot be null or empty. It's required to build post policy");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("uploadFileName cannot be null or empty. It's required to build post policy");
}
if (string.IsNullOrEmpty(contentType))
{
throw new ArgumentException("contentType cannot be null or empty. It's required to build post policy");
}
string policyString = null;
int position = key.LastIndexOf('/');
if (position == -1)
{
policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" +
bucketName + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}";
}
else
{
policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" +
bucketName + "\"},[\"starts-with\", \"$key\", \"" + key.Substring(0, position) + "/\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + contentType + "\"" + "]]}";
}
return policyString;
}
}
下面为.Net的上传物体可用代码:
//private static readonly string awsAccessKey = "*****************";
//private static readonly string awsSecretKey = "***************************";
private static readonly string awsAccessKey = "**********************************";
private static readonly string awsSecretKey = "**************************";
private static readonly string bucketName = "************";
//private static readonly string bucketName = "****************";
static AmazonS3Config config = new AmazonS3Config()
{
ServiceURL = "http://s3.amazonaws.com"
};
static AmazonS3Client client;
//static AmazonS3Client amazonS3Client;
static void Main(string[] args)
{
//FileStream stream = File.OpenRead(args[0]);
FileStream stream = File.OpenRead(@"D:\1107.jpg");
//string resourcePath = @"D:\Demo\002.jpg";
string info;
//CreateBucket("test");
//GetBucketList();
//UploadbyPath(resourcePath);
//Download(args[0]);
using (AmazonS3Client amazonS3Client = new AmazonS3Client(awsAccessKey, awsSecretKey, config))
{
PutObjectRequest request = new PutObjectRequest()
{
BucketName = bucketName,
//FilePath = args[0],
InputStream = stream,
CannedACL = S3CannedACL.PublicReadWrite,
Key = Path.GetFileName("123")
//ContentType = "text/plain"
};
try
{
amazonS3Client.PutObject(request);
info = "success";
//amazonS3Client.Dispose();
}
catch (AmazonS3Exception ex)
{
info = "failed:" + ex.Message;
}
//TransferUtility transfer = new TransferUtility(amazonS3Client);
//transfer.Upload(path, bucketName, Path.GetFileName(path));
}
//stream.Close();
//stream.Dispose();
//amazonS3Client.Dispose();
//return args[0];
Console.WriteLine(info);
Console.ReadKey();
}