S3Client :: GetObject与SetRange返回错误的数据?

时间:2019-05-29 22:18:55

标签: c++ amazon-web-services amazon-s3

我正在尝试从前1048576个字节开始读取大型AWS S3对象的一段。我指定了我要读取的字节范围,但没有收到这些字节。返回的长度正确。

我在这方面还很陌生,所以我可能做错了什么,而成为C程序员而不是C ++并没有太大帮助。

这是我的代码示例:

int   start=0, rbytes=0;

Aws::S3::S3Client  s3_client;
Aws::S3::Model::GetObjectRequest read_object_request;
sprintf(range,"bytes=%lld-%lld",start,start+1048575);

read_object_request.SetBucket(container_name);
read_object_request.SetKey(object_name);
read_object_request.SetRange(range);
auto results = s3_client.GetObject(read_object_request);
if (results.IsSuccess())
{
   rbytes = results.GetResult().GetContentLength();
   std::cout << rbytes << " bytes read" << std::endl;
}
memcpy(bdata,results.GetResult().GetBody().rdbuf(),rbytes);

然后我转储bdata缓冲区,这不是我期望的。当我重复执行代码时,一个新实例将返回不同的数据。

尽管我没有显示bdata缓冲区已预分配2 MB。

任何能为我指明正确方向的想法都会受到赞赏。

谢谢。

2 个答案:

答案 0 :(得分:1)

rdbuf()是流,您将其视为内存指针。

答案 1 :(得分:1)

这是我对感兴趣的任何人的最终代码。如果您认为有任何建议,我也将不胜感激。

#include <streambuf>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/s3/model/HeadObjectRequest.h>
#include <aws/s3/model/HeadObjectResult.h>
#include <aws/s3/model/Object.h>

long scanObject(Aws::String region,
                Aws::String bucket_name,
                Aws::String object_name);


int main(int argc, char** argv)
{
   long scanned=0;

   if (argc < 4){ return(1); }
   Aws::String region = argv[1];
   Aws::String bucket_name = argv[2];
   Aws::String object_name = argv[3];

   Aws::SDKOptions options;
   Aws::InitAPI(options);

   scanned = scanObject(region,bucket_name,object_name);
   printf("Scanned %d bytes of object\n",scanned);

   Aws::ShutdownAPI(options);

   return(0);
}

long scanObject(Aws::String region,
                Aws::String bucket_name,
                Aws::String object_name)
{
   int   rbytes, nbytes;
   char range[48], *buffer;
   long filesize, bytesleft, start;

   Aws::Client::ClientConfiguration cconfig;
   cconfig.region = region;
   Aws::S3::S3Client s3_client(cconfig);

   Aws::S3::Model::HeadObjectRequest head_object_request;
   head_object_request.WithBucket(bucket_name).WithKey(object_name);
   Aws::S3::Model::HeadObjectOutcome head_object;
   head_object = s3_client.HeadObject(head_object_request);
   if (!head_object.IsSuccess())
   {
      printf("AWS Object, /%s/%s, failed to locate\n",
             region.c_str(),object_name.c_str());
      return(-1);
   }

   filesize = head_object.GetResult().GetContentLength();
   bytesleft = filesize;

   start = 0;
   nbytes = 1048576;
   buffer = (char*)malloc(nbytes*2);

   Aws::S3::Model::GetObjectRequest read_request;
   while(bytesleft > 0)
   {
      if (bytesleft > 1048576){ nbytes = 1048576; }else{ nbytes = bytesleft; }

      sprintf(range,"bytes=%d-%d",start,start+nbytes-1);

      read_request.SetBucket(bucket_name);
      read_request.SetKey(object_name);
      read_request.SetRange(range);

      auto results = s3_client.GetObject(read_request);

      if (!results.IsSuccess())
      {
         printf("\nUnable to fetch object range %s from s3 bucket.\n",range);
         return(-1);
      }
      rbytes = results.GetResult().GetContentLength();

      std::streambuf* body = results.GetResult().GetBody().rdbuf();
      body->sgetn(buffer,rbytes);

      start += rbytes;
      bytesleft -= rbytes;
   }

   return(filesize);
}

我相信这可以通过更有效的方式来完成,希望我会继续学习。