首页 > 编程语言 >[996] Upload a file to an Amazon S3 bucket and obtain the URL of it using Python

[996] Upload a file to an Amazon S3 bucket and obtain the URL of it using Python

时间:2024-05-14 14:10:02浏览次数:18  
标签:996 S3 AWS bucket URL file your

Certainly! To upload a file to an Amazon S3 bucket using Python (specifically with the boto3 library) and obtain the link (URL) for that file, follow these steps:

  1. Install Boto3: Make sure you have the boto3 library installed. If not, you can install it using pip:

    pip install boto3
  2. Create an S3 bucket: Refer to this link: Store and Retrieve a File with Amazon S3

  3. Install the AWS Command Line Interface (AWS CLI): Refer to this link: AWS CLI install and update instructions

  4. Configure the AWS Access Key and Secret Access Key: Refer to this link: How to create AWS access key id & secret key
  5. Upload the File to S3: Use the following code snippet to upload a file to your S3 bucket:

    import boto3
    
    # Initialize the S3 client
    s3 = boto3.client('s3')
    
    # Specify your S3 bucket name and the key (object name) for the file
    bucket_name = 'your-bucket-name'
    file_key = 'file.xlsx'  # Replace with your desired object key
    
    # Upload the file (assuming the file is in the current directory)
    s3.upload_file('file.xlsx', bucket_name, file_key)
  6. Generate the URL (Presigned URL): To get the URL for the uploaded file, you can create a presigned URL. This URL allows temporary access to the file. Here’s how to generate it:

    # Generate a presigned URL for the uploaded file
    url = s3.generate_presigned_url(
        ClientMethod='get_object',
        Params={
            'Bucket': bucket_name,
            'Key': file_key
        },
        ExpiresIn=3600  # Set the expiration time (in seconds) as needed
    )
    
    print(f"Presigned URL: {url}")

    The ExpiresIn parameter specifies how long the URL will be valid (e.g., 3600 seconds = 1 hour). Adjust it according to your requirements.

  7. Using the URL: The url variable now contains the presigned URL for your file. You can share this URL with others, and they’ll be able to download the file directly from S3.

Remember to replace 'your-bucket-name' and 'file.xlsx' with your actual bucket name and file key. Additionally, ensure that your AWS credentials are properly configured (either via environment variables or AWS configuration files).

Feel free to customize the code based on your specific use case!

标签:996,S3,AWS,bucket,URL,file,your
From: https://www.cnblogs.com/alex-bn-lee/p/18191175

相关文章

  • ESP32-S3-WROOM-1 arduino测试选项
    1、2、3、 ......
  • FFmpeg开发笔记(二十)Linux环境给FFmpeg集成AVS3解码器
    ​AVS3是中国AVS工作组制定的第三代音视频编解码技术标准,也是全球首个已推出的面向8K及5G产业应用的视频编码标准。AVS工作组于2019年3月9日完成第三代AVS视频标准(AVS3)基准档次的制订工作,参考软件的测试表明,AVS3基准档次的性能比上一代标准AVS2和HEVC提升了约30%。libuavs3d是AVS......
  • 204-基于Xilinx Virtex-6 XC6VLX240T 和TI DSP TMS320C6678的信号处理板
    基于XilinxVirtex-6XC6VLX240T和TIDSPTMS320C6678的信号处理板 1、板卡概述    板卡由北京太速科技自主研发,基于VPX架构,主体芯片为两片TIDSPTMS320C6678,两片Virtex-6XC6VLX240T-ff1156FPGA,1个RapidIOSwitch。FPGA连接FMC子卡。FPGA......
  • 1-Maven-Nexus3
    1.Maven&Nexus3maven是java编写的开源项目管理工具,采用POM(ProjectObjectModel)来管理项目,所有项目配置信息被保存在POM.xml文件中,通过该文件Maven可以管理整个项目的生命周期私服是架设在局域网的一种特殊远程仓库,目的是代理远程仓库和第三方构件。有了私服务,当Maven需要下载......
  • 学习记录+vcode+GPIO例程+正点原子 DNESP32S3 开发板教程-IDF 版
    第一个程序:UART模式和JTAG模式,配置完成不同。配置主要就是.vscode文件夹中 c_cpp_properties.json,tasks.json,launch.json,settings.json四个文件。一个想法:备份UART模式和JTAG模式的配置文件,用时直接文件替换。简单粗暴方式是.vscode文件夹替换。当然每次要选好串口、设置目标......
  • 我买的键盘说明书 高斯 GS3104T-LI
    保修期一年键盘右上角四个小灯是什么意思???第一个是数字锁定指示灯(NumLock)第二个是大写锁定指示灯(CapsLock)第三个是滚动条锁定指示灯(ScrollLock)第四个是windows键锁定指示灯 Windows键,简称“Winkey”或“Win键”,是在计算机键盘左下角 Ctrl 和 Alt 键之间的按键,台式机全......
  • 挑战前端基础120题--css3基础篇
    1. CSS选择器种类、权重、优先级计算、匹配顺序、继承?!important>行内样式>id>类选择器,伪类(:hover),属性选择器>标签,伪元素选择器(::before::after:root)>通配符,继承,关系选择器(兄弟选择器(子选择器(>),相邻选择器(+)>浏览器默认能够被继承的属性:font-size/f......
  • AWS S3 Lambda Python脚本函数实现图片自动转换为webp并上传至s3
    AmazonS3自动转换图片格式 AmazonS3存储桶新增文件自动触发AWSLambda。Lambda取S3文件做转换并存回去S3同一个目录下,并增加相应的后缀名。并且支持通过APIGateway的方式触发对图片进行修改并输出。本Lab使用PythonPillow做图片转换,读者可以参考Pillow文档......
  • 使用 AWS CLI 操作 S3
    使用cp命令将本地文件拷贝到S3,或者将S3中的文件拷贝到本地。awss3cplocal-file-paths3://your-bucket-name/optional-folder-path/remote-file-namelocal-file-path:您的本地机器上的文件路径,比如C:/Documents/example.txt或者/home/user/example.txt。your-bu......
  • 使用 AWS CLI 开发 Amazon S3
    AmazonS3:AmazonSimpleStorageServiceawss3ls参考:s3命令解释:s3操作S3(上传对象、下载对象):s3apiS3管理(创建桶、删除桶):s3control......