| 442 | } |
| 443 | |
| 444 | Status S3FileSystem::Stat(const string& fname, FileStatistics* stats) { |
| 445 | string bucket, object; |
| 446 | TF_RETURN_IF_ERROR(ParseS3Path(fname, true, &bucket, &object)); |
| 447 | |
| 448 | if (object.empty()) { |
| 449 | Aws::S3::Model::HeadBucketRequest headBucketRequest; |
| 450 | headBucketRequest.WithBucket(bucket.c_str()); |
| 451 | auto headBucketOutcome = this->GetS3Client()->HeadBucket(headBucketRequest); |
| 452 | if (!headBucketOutcome.IsSuccess()) { |
| 453 | return errors::Unknown(headBucketOutcome.GetError().GetExceptionName(), |
| 454 | ": ", headBucketOutcome.GetError().GetMessage()); |
| 455 | } |
| 456 | stats->length = 0; |
| 457 | stats->is_directory = 1; |
| 458 | return Status::OK(); |
| 459 | } |
| 460 | |
| 461 | bool found = false; |
| 462 | |
| 463 | Aws::S3::Model::HeadObjectRequest headObjectRequest; |
| 464 | headObjectRequest.WithBucket(bucket.c_str()).WithKey(object.c_str()); |
| 465 | headObjectRequest.SetResponseStreamFactory( |
| 466 | []() { return Aws::New<Aws::StringStream>(kS3FileSystemAllocationTag); }); |
| 467 | auto headObjectOutcome = this->GetS3Client()->HeadObject(headObjectRequest); |
| 468 | if (headObjectOutcome.IsSuccess()) { |
| 469 | stats->length = headObjectOutcome.GetResult().GetContentLength(); |
| 470 | stats->is_directory = 0; |
| 471 | stats->mtime_nsec = |
| 472 | headObjectOutcome.GetResult().GetLastModified().Millis() * 1e6; |
| 473 | found = true; |
| 474 | } |
| 475 | string prefix = object; |
| 476 | if (prefix.back() != '/') { |
| 477 | prefix.push_back('/'); |
| 478 | } |
| 479 | Aws::S3::Model::ListObjectsRequest listObjectsRequest; |
| 480 | listObjectsRequest.WithBucket(bucket.c_str()) |
| 481 | .WithPrefix(prefix.c_str()) |
| 482 | .WithMaxKeys(1); |
| 483 | listObjectsRequest.SetResponseStreamFactory( |
| 484 | []() { return Aws::New<Aws::StringStream>(kS3FileSystemAllocationTag); }); |
| 485 | auto listObjectsOutcome = |
| 486 | this->GetS3Client()->ListObjects(listObjectsRequest); |
| 487 | if (listObjectsOutcome.IsSuccess()) { |
| 488 | if (listObjectsOutcome.GetResult().GetContents().size() > 0) { |
| 489 | stats->length = 0; |
| 490 | stats->is_directory = 1; |
| 491 | found = true; |
| 492 | } |
| 493 | } |
| 494 | if (!found) { |
| 495 | return errors::NotFound("Object ", fname, " does not exist"); |
| 496 | } |
| 497 | return Status::OK(); |
| 498 | } |
| 499 | |
| 500 | Status S3FileSystem::GetMatchingPaths(const string& pattern, |
| 501 | std::vector<string>* results) { |