public function getDocumentBucket(string $className) : Bucket
    {
        $className = $this->classNameResolver->getRealClass($className);

        /** @var ClassMetadata $metadata */
        $metadata = $this->metadataFactory->getMetadataFor($className);
        if (! $metadata->isFile) {
            throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className);
        }

        $bucketName = $metadata->getBucketName();

        if (! $bucketName) {
            throw MongoDBException::documentNotMappedToCollection($className);
        }

        if (! isset($this->documentBuckets[$className])) {
            $db = $this->getDocumentDatabase($className);

            $options = ['bucketName' => $bucketName];
            if ($metadata->readPreference !== null) {
                $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags);
            }

            $this->documentBuckets[$className] = $db->selectGridFSBucket($options);
        }

        return $this->documentBuckets[$className];
    }