我正在将不同类型的namespace AWSSDK.Examples
{
public class S3Example : MonoBehaviour
{
public bool test;
public string IdentityPoolId = "";
public string CognitoIdentityRegion = RegionEndpoint.APNortheast2.SystemName;
private RegionEndpoint _CognitoIdentityRegion
{
get { return RegionEndpoint.GetBySystemName(CognitoIdentityRegion); }
}
// ap-northeast-2:7502e180-f5b2-47c4-9843-b49c592b890e
// ap-northeast-2:7502e180-f5b2-47c4-9843-b49c592b890e
public string S3Region = RegionEndpoint.APNortheast2.SystemName;
private RegionEndpoint _S3Region
{
get { return RegionEndpoint.GetBySystemName(S3Region); }
}
public string S3BucketName = null;
public string SampleFileName = null;
public Button GetBucketListButton = null;
public Button PostBucketButton = null;
public Button GetObjectsListButton = null;
public Button DeleteObjectButton = null;
public Button GetObjectButton = null;
public Text ResultText = null;
void Start()
{
Debug.Log(IdentityPoolId);
UnityInitializer.AttachToGameObject(this.gameObject);
AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
GetBucketListButton.onClick.AddListener(() => { GetBucketList(); });
PostBucketButton.onClick.AddListener(() => { PostObject(); });
GetObjectsListButton.onClick.AddListener(() => { GetObjects(); });
DeleteObjectButton.onClick.AddListener(() => { DeleteObject(); });
GetObjectButton.onClick.AddListener(() => { GetObject(); });
}
#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()
{
ResultText.text = "Fetching all the Buckets";
Client.ListBucketsAsync(new ListBucketsRequest(), (responseObject) =>
{
ResultText.text += "\n";
if (responseObject.Exception == null)
{
ResultText.text += "Got Response \nPrinting now \n";
responseObject.Response.Buckets.ForEach((s3b) =>
{
ResultText.text += string.Format("bucket = {0}, created date = {1} \n", s3b.BucketName, s3b.CreationDate);
});
}
else
{
ResultText.text += "Got Exception \n";
}
});
}
#endregion
/// <summary>
/// Get Object from S3 Bucket
/// </summary>
private void GetObject()
{
ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
{
string data = null;
var response = responseObj.Response;
//if (response.ResponseStream != null)
//{
// using (StreamReader reader = new StreamReader(response.ResponseStream))
// {
// Debug.Log(reader);
// data = reader.ReadToEnd();
// }
// ResultText.text += "\n";
// ResultText.text += data;
//}
//else
//{
// ResultText.text += "\n";
// ResultText.text += "Error";
//}
// file Download
if (response.ResponseStream != null)
{
using (var fs = System.IO.File.Create(Application.persistentDataPath + Path.DirectorySeparatorChar + SampleFileName))
{
byte[] buffer = new byte[81920];
int count;
while ((count = response.ResponseStream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, count);
}
fs.Flush();
}
}
Debug.Log("Success");
});
}
/// <summary>
/// Post Object to S3 Bucket.
/// </summary>
public void PostObject()
{
ResultText.text = "Retrieving the file";
string fileName = GetFileHelper();
var stream = new FileStream(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
ResultText.text += "\nCreating request object";
var request = new PutObjectRequest()
{
BucketName = S3BucketName,
Key = "test2/" + fileName,
InputStream = stream,
CannedACL = S3CannedACL.Private
};
ResultText.text += "\nMaking HTTP post call";
Client.PutObjectAsync(request, (responseObj) =>
{
if (responseObj.Exception == null)
{
ResultText.text += string.Format("\nobject {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.BucketName);
}
else
{
ResultText.text += "\nException while posting the result object";
ResultText.text += string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString());
}
});
}
/// <summary>
/// Get Objects from S3 Bucket
/// </summary>
public void GetObjects()
{
ResultText.text = "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) =>
{
ResultText.text += string.Format("{0}\n", o.Key);
});
}
else
{
ResultText.text += "Got Exception \n";
}
});
}
/// <summary>
/// Delete Objects in S3 Bucket
/// </summary>
public void DeleteObject()
{
ResultText.text = string.Format("deleting {0} from bucket {1}", SampleFileName, S3BucketName);
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) =>
{
ResultText.text += dObj.Key;
});
}
else
{
ResultText.text += "Got Exception \n";
}
});
}
}
}
的每个键和值转换为字符串。
我尝试了以下代码:
dict
我们必须像下面表示的那样在def convert_dict_to_str_dict(data_dict):
converted_dict = {}
for key, value in data_dict.items():
if isinstance(value, dict):
converted_dict[str(key)] = convert_dict_to_str_dict(value)
else:
converted_dict[str(key)] = str(value)
return converted_dict
data_dict = {
1: 123,
'string' : 'this is a string',
'data' : {
123: 2345,
'we' : {
1: 100,
2: 'two hundred'
},
345 : 15427
}
}
上方进行转换。
dict