Downloaded file from storj

I uploaded a text file to my Storj bucket at a specific upload path.

Now, I want to download this file, which I uploaded, so I wrote the following lines:

// Open file
obj, err := bucket.OpenObject(ctx, “test/log.txt”)
if err != nil {
return err
}
defer errCatch(obj.Close)

// Get a reader for the entire file
r, err := obj.DownloadRange(ctx, 0, -1)
if err != nil {
    return err
}
defer errCatch(r.Close)

Now, I just want that the los.txt which I uploaded earlier gets downloaded to my present working directory.

By using ioutil, I am only able to read the file and print it.

Please guide me.

1 Like

First every function is already implemented in uplink you could take examples from there. Copy

You could extend your example with

var reader io.ReadCloser
reader = r

file, err := os.Create("filename")
if err != nil {
		fmt.Println("Error creating file")
		return
	}
defer file.Close()

_, err = io.Copy(file, reader)
	if err != nil {
		return err
	}

WARNING i wrote this just down from scratch, it is untested but should write your output to a file. If this already exist it will be overwritten. You need to extend this for you needs.

1 Like

@BlackDuck Thank you :smiley::smiley:

1 Like