尝试将目录上载到S3(.NET API-C#)时出现NullReferenceException
本文关键字:API-C# NET NullReferenceException S3 上载 | 更新日期: 2023-09-27 18:29:15
浏览完网页后,我完全陷入了一个正在构建的应用程序中,该应用程序使用C#(Targeting.NET 4.5)将目录推送到Amazon S3。在使用TransferManager类的UploadDirectory(TransferUtilityUploadDirectoryRequest)方法推送目录文件的代码行中,我得到了NullReferenceException。问题是我找不到任何空的!调试器也没有显示任何null,所以我显然在这里缺少了一些东西。
我读到,如果你要上传到有句点的bucket,你需要将协议更改为HTTP,否则可能会引发NullReferenceException,但我也这样做了,并且继续收到错误,即使我创建了另一个没有句点的测试bucket。
我的代码直到&包括导致异常的行如下。名为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行是上面提到的行。
今天早些时候发布了修补版SDK(1.5.30.1版),修复了此问题。
我在AWS论坛上发布了同样的问题。显然,在.NET SDK的1.5.30版本中,方法UploadDirectory()
的API已损坏。
亚马逊刚刚发布了一个补丁,版本为1.5.30.1