Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating Auth Token and re-using it for list of image files stored on B2 bucket #68

Open
StefanCepa opened this issue Sep 17, 2019 · 0 comments

Comments

@StefanCepa
Copy link

Hello, I am trying to generate Authorization token which I would like to re-use in order to grant tmp access to image files that I am storing on B2 bucket.

Here is my code:

type B2 struct {
	ID         string          // unique identifier for B2 cloud storage
	Key        string          // application key that controls access to B2 storage
	BucketName string          // name of the container that holds files
	ObjectKey  string          //  absolute path to specific file/directory inside given bucket
	AuthToken  string          // authentication token which is used in order to grant access to bucket private files
	Client     *b2.Client      // b2 client object
	Context    context.Context // environment context
	Bucket     *b2.Bucket      // b2 bucket object
}

//
// Creates and initializes new B2 struct.
//
// Arguments:
//   - id string: unique identifier for B2 cloud storage
//   - key string: application key that controls access to B2 storage
//   - bucket string: name of a bucket on b2 cloud storage
//   - objectKey string: path to specific file or directory that is part of B2 bucket
//
// Returns:
//   - Reference to new B2 instance
//
func NewB2Driver(id string, key string, bucketName string, objectKey string) (*B2, error) {
	var b2Driver B2

	b2Driver.Context = context.Background()

	client, err := newB2Client(&b2Driver.Context, id, key)
	if err != nil {
		glog.Errorf("Failed to create new B2 client object, reason - %v", err)
		return nil, err
	}
	b2Driver.Client = client

	bucket, err := newB2Bucket(&b2Driver.Context, client, bucketName, objectKey)
	if err != nil {
		glog.Errorf("Failed to create new B2 bucket object, reason - %v", err)
		return nil, err
	}
	b2Driver.Bucket = bucket

	b2Driver.ID = id
	b2Driver.BucketName = bucketName
	b2Driver.Key = key
	b2Driver.ObjectKey = objectKey

	return &b2Driver, nil
}

func (b2Obj *B2) GenerateTmpAuthURL(prefix string, expireAfter time.Duration) (string, error) {
	if b2Obj.AuthToken == "" {
		authToken, err := b2Obj.Bucket.AuthToken(b2Obj.Context, prefix, expireAfter)
		if err != nil {
			glog.Errorf("Failed to generate temporary authorization token for B2 cloud storage, reason - %v", err)
			return "", err
		}

		glog.V(2).Infof("[INFO]: Successfully generated tmp authorization token for B2 cloud storage. Token - %s", authToken)
		b2Obj.AuthToken = authToken
	}

	objUrl := b2Obj.Bucket.Object(b2Obj.ObjectKey).URL()
	objUrl = objUrl + "?Authorization=" + b2Obj.AuthToken

	return objUrl, nil
}

And here is how resulting URL looks like once functions above are executed:
https://f003.backblazeb2.com/file/cart-test-thumbnail-lambda-thumbnail-bucket/thumbnail_20181231235907_0_181094_0.jpeg?Authorization=3_20190917124617_ed5bc772862c3ab6ada9a331_0ed4df528f58a6e91bdee9dbb96a7425501100b6_003_20190917125117_0084_dnld

When I copy paste URL into the browser I get the following:
image

Is this suppose to happen? Shouldnt I have access to the file without having to SIGN IN? Also, whenever I enter my information I get back right to the same pop-up window, nothing happens. Its like I cannot sign in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant