Home > OS >  Downloading Files From S3 To Users Computer
Downloading Files From S3 To Users Computer

Time:04-29

I have the following view in my Django project

class download_mscore_software(APIView):
    def get(self, request):
        session = boto3.Session(
            aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY
        )
        s3 = session.resource('s3')

        clancy = s3.Bucket("magpie-bixly-test").download_file("Clancy-Gorillaz.jpg", "Clancy-Gorillaz.jpg")

This works to download the file into my project directory. However, I need the file to download to the users computer. How do I change this so that this happens? I tried this:

clancy = s3.Bucket('magpie-bixly-test').download_file('Clancy-Gorillaz.jpg', 'Users/Downloads/Clancy-Gorillaz.jpg')

but I get an error saying that there is no such directory. The same happens if I leave the file name off the end like this.

clancy = s3.Bucket('magpie-bixly-test').download_file('Clancy-Gorillaz.jpg', 'Users/Downloads')

CodePudding user response:

My guess is that the specified paths are relative to your project and thus the Users/Downloads folder isn’t found. You can try specifying the absolute path (Windows: C:/Users/<USERNAME>/Downloads/Clancy-Gorillaz.jpg or Linux: /home/<USERNAME>/Downloads/Clancy-Gorillaz.jpg). Where <USERNAME> should be replaced with the OS user.

Even if it works a good practice is to use os.path to work with a particular OS.

  • Related