在搜索了网络之后,我完全陷入了我正在构建的应用程序上,该应用程序使用 C#(目标 .NET 4.5)将目录推送到 Amazon S3。我在使用 TransferManager 类的 UploadDirectory(TransferUtilityUploadDirectoryRequest) 方法推送目录文件的代码行上得到 NullReferenceExceptions。问题是我找不到任何为空的东西!调试器也没有显示任何空值,所以我显然在这里遗漏了一些东西。
我读到,如果您要上传到其中有句点的存储桶,则需要将协议更改为 HTTP,否则可能会引发 NullReferenceException,但是我也这样做了,并且继续收到错误,即使我创建了另一个没有句点的测试桶。
我的代码部分直到 & 包括导致异常的行如下。名为 S3Info 的类只是我创建的一个帮助类,它只存储一些配置信息,例如访问/密钥和其他信息:
public static void uploadDirectories(S3Info info, List<DirectoryInfo> dirs, Logger logger = null)
{
AmazonS3Config alterConfig = new AmazonS3Config();
alterConfig.CommunicationProtocol = Protocol.HTTP;
AmazonS3Client s3Client = new AmazonS3Client(info.getCredentials(), alterConfig);
TransferUtility directoryTransferUtil = new TransferUtility(s3Client);
TransferUtilityUploadDirectoryRequest uploadDirRequest;
PutObjectRequest completeFileUploadRequest;
uint uploadSuccessCount = 0;
if (dirs == null || dirs.Count == 0)
{
logger.log("Nothing to upload.");
return;
}
//upload directory with PDFs
foreach (DirectoryInfo dir in dirs)
{
try
{
//configure upload request
uploadDirRequest = new TransferUtilityUploadDirectoryRequest();
uploadDirRequest.BucketName = info.selectedBucket.BucketName;
uploadDirRequest.Directory = dir.FullName;
uploadDirRequest.KeyPrefix = dir.Name + @"\";
uploadDirRequest.SearchOption = SearchOption.TopDirectoryOnly;
uploadDirRequest.SearchPattern = "*.pdf";
uploadDirRequest.Timeout = 600000; //10 minutes
//upload directory!
directoryTransferUtil.UploadDirectory(uploadDirRequest); //exception thrown here
在这一点上我有点卡住了,所以我愿意接受社区可以提供的任何建议。谢谢。
编辑:堆栈跟踪-
Object reference not set to an instance of an object. :
at Amazon.S3.Transfer.Internal.UploadDirectoryCommand.Execute()
at Amazon.S3.Transfer.TransferUtility.UploadDirectory(TransferUtilityUploadDirectoryRequest request)
at S3Delivery.AmazonActions.uploadDirectories(S3Info info, List`1 dirs, Logger logger) in c:\Users\jblacker\Documents\Visual Studio 2012\Projects\S3Delivery\S3Delivery\AmazonActions.cs:line 173
第 173 行是上面提到的行。