Unable To Access Shared Link - Golang

am trying to fetch all the images in the folder I uploaded to storj test bucket but am unable to show the shared image in the browser as a read-only

below is the code showing the implementation

 func (s *storjFileStorageSvc) ListItemFiles(user int64, itemID int32) ([]string, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	folder := fmt.Sprintf("%v/%v/", user, itemID)
	iterator := s.project.ListObjects(ctx, s.config.Storj.BucketName, &uplink.ListObjectsOptions{
		Prefix:    folder,
		Recursive: true,
	})
	var shareableLinks []string
	for iterator.Next() {
		object := iterator.Item()
		// Restrict the permissions to only allow read access to the specific object
		permission := uplink.ReadOnlyPermission()
		shared := uplink.SharePrefix{Bucket: s.config.Storj.BucketName, Prefix: folder}
		restrictedAccess, err := s.access.Share(permission, shared)
		if err != nil {
			return nil, err
		}

		serializedAccess, err := restrictedAccess.Serialize()
		if err != nil {
			return nil, err
		}
		link := fmt.Sprintf("%s/%s/%s", s.config.Storj.ShareEndpoint, serializedAccess, object.Key)
		shareableLinks = append(shareableLinks, link)
	}

	if err := iterator.Err(); err != nil {
		return nil, err
	}

	return shareableLinks, nil
}

the generated link is as follows → https://link.storjshare.io/s/1Hqw6c4YqvedwUWmQvJZvtjezcE67YbNyCDfZawo4bEGjywVqT8Ny3aYNep4ZynTcr4dnUSovPUWjC3X1MdkgF5FBMhru92eD5vNhFg8J2xFMt4eBEDKVBzaP6qmDejp5s8pN4fcCCHBmXkQrVUZUBCQt6Vme8QgZit6RgHXE1je1hzM589GjUygUvUAiEsfcrvzqNmsdo9HxAbUX54hBGNfxa8gwpqvVxcxbrWuyqZEzpGK3pmm18kJWVdTSVScio49wB8QGogUcAsGmq7peGAY4wvMkKmenEokmCERXavvhZnfsLsUE3Qc9WFinNemMZAuFTnxERmpWBD1bKg1DyhV1Enpj2tXaFqRwPr4ZEfM4HV6bqybzLxzpUyh9niFov8eWz1Jg7bXca4MwNhGzXB4DFXFFZCvUyrGdiM6ciDtB35CsM5aXiPcnoqbEqSGypZ5FXHCxCDAJUQMZtHtqEHcBoEmeNMH4tcfhButa5x5cTDWHq7ZsyviM2xuzJnALg/2/2/image2.png

Two issues:

The main issue is that your URL generation needs the bucket name before the object key. Otherwise your access grant generation is pretty good!

A secondary issue though is you should not (yet) use access grants for URLs like this since they are essentially blank checks for bandwidth usage from your account. It is better to register an access grant and get a shortened access key, which also reduces the risk of unsavory folks abusing your account. You can use this package for doing so: edge package - storj.io/uplink/edge - Go Packages

5 Likes