0

I've been trying to download a file through this view after users pay for it, but i can't seem to make it work. i first tried using file response

urls.py

    urlpatterns = [
        path('download-files/<int:id>/<str:order_id>/', download_file, name='download_file'),
     ]

models.py

class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    file = models.FileField(upload_to="files", null=True)

views.py

    def download_file(request, id, order_id):
        obj = Product.objects.get(id=id)
        order = Order.objects.get(order_id=order_id)
        file = obj.file
        response = FileResponse(open(file, 'rb'))
        return response

But all i kept getting was this error

venv\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'bool' object has no attribute 'get'

I tried using boto3 to download the file

viewa.py

    def download_file(request, id, order_id): 
        obj = Product.objects.get(id=id)
        order = Order.objects.get(order_id=order_id)
        file = obj.file

        client = boto3.client('s3', aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID'), aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY'))
        bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME')
        # url = 'https://' + settings.AWS_S3_CUSTOM_DOMAIN + '/media/' + f'{file.name}'
           

        url = client.generate_presigned_url('get_object', Params = {'Bucket': bucket_name, 'Key': file.name, }, ExpiresIn = 600, )
        return HttpResponseRedirect(url)

and i go this error

    <Error>
    <Code>NoSuchKey</Code>
    <Message>The specified key does not exist.</Message>
    <Key>files/it_report_edited.docx</Key>
    <RequestId>JR7XHE6A8D7F2872</RequestId <HostId>LiQyKEyHsw/xerU2lJXTEZG+m3obOoUdlVH06d2vE+KHHWQVspOqOb9mN9JmJqBW7DVb8Gc63uZmuLSVqcvbYO7SJ3GNLbzlvNnVAxLOvls=</HostId>
</Error>

It looks to me that i'm not giving the correct key that's why it can't find it, is there a coorect way to handle this? i've tried everything

Quest
  • 11
  • 4
  • Can you share the complete object path? Also, see this: https://stackoverflow.com/questions/28653249/amazon-s3-exception-the-specified-key-does-not-exist – Abdullah Khawer Mar 31 '23 at 10:01
  • obj.file.path should display the complete object path, but when using aws s3 bucket then what works is obj.file – Quest Mar 31 '23 at 14:48
  • Note: you don't need to explicitly provide AWS credential environment variables when creating a boto3 client. All AWS SDKs will read them as part of their default credential provider chain. – jarmod Mar 31 '23 at 14:48
  • The original `AttributeError: 'bool' object has no attribute 'get'` error is because you are misusing the [clickjacking middleware](https://docs.djangoproject.com/en/4.1/_modules/django/middleware/clickjacking/) and have somehow replaced a response object with a boolean value. – jarmod Mar 31 '23 at 14:53
  • @jarmod should I remove the clickjacking middleware un my settings.py? and how did I change the response object....is there a way i can prevent this – Quest Mar 31 '23 at 15:35
  • If you're using the clickjacking middleware then review the docs for how to use it correctly. If you're using it correctly then report a bug to the maintainers. Or remove it entirely. But I presume you want this functionality. – jarmod Mar 31 '23 at 15:39

0 Answers0