Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use AWS S3 in Laravel

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points about how AWS S3 is used in Laravel. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

AWS S3 provides us with a place to store server files. The advantages of this are:

Backup / redundancy-S3 and similar products have built-in backups and redundancy

Extension-in modern servers (such as serverless or containerized environments and traditional load balancing environments), saving files outside the server becomes necessary

Disk utilization-does not require too much disk space when storing files in the cloud

Features-S3 (and other clouds) have some great features, such as version control of files, lifecycle rules for deleting old files (or storing them more cheaply), deletion protection, etc.

Using S3 now (even in a single-server setup) can reduce hassle in the long run. That's what you should know!

Configuration

Configuring S3 requires the following:

In Laravel-usually through .env, but may also be in config/filesystem.php

Your AWS account

Laravel Config

If you check your config/filesystem.php file, you will find that you already have the S3 option. It has been set to the environment variable in the .env file!

Unless you need to customize this, you can ignore it and just set it in the .env file:

# (optional) set the default file system driver to S3FILESYSTEM_DRIVER=sqs# to add the parameter AWS_ACCESS_KEY_ID=xxxzzzAWS_SECRET_ACCESS_KEY=xxxyyyAWS_DEFAULT_REGION=us-east-2AWS_BUCKET=my-awesome-bucketAWS_USE_PATH_STYLE_ENDPOINT=false required for S3-based file drivers

The config/filesystem.php file contains the following options:

Return ['disks' = > [/ /' local' and 'public' are omitted. 's3' = > ['driver' = >' s3', 'key' = > env (' AWS_ACCESS_KEY_ID'), 'secret' = > env (' AWS_SECRET_ACCESS_KEY'), 'region' = > env (' AWS_DEFAULT_REGION'), 'bucket' = > env (' AWS_BUCKET'), 'url' = > env (' AWS_URL') 'endpoint' = > env (' AWS_ENDPOINT'), 'use_path_style_endpoint' = > env (' AWS_USE_PATH_STYLE_ENDPOINT', false),],]

There are some options we don't use in the .env file. For example, you can set up AWS_URL, which is useful for storing clouds using other files with S3-compatible API, such as R2 for CloudFlare or Spaces for Digital Ocean.

AWS configuration

In AWS, you need to do two things:

Create a bucket in the S3 service

Create an IAM user to get Key and Secret Key, and then attach a policy to that user to allow access to S3 API.

Like anything in AWS, creating buckets in S3 requires looking at a number of configuration options and wondering if you need any of them. For most use cases, you don't need it!

Go to the S3 console, create a bucket name (it must be globally unique, not just your AWS account unique), select the area you are operating on, and leave all default values (including the area marked "prevent Public access Settings").

Some of these options are options that you may want to use, but you can select them later.

After creating the bucket, we need permission to perform operations on it. Suppose we create a bucket called my-awesome-bucket.

We can create an IAM user and select programmatic access, but do not attach any policies or set anything else. Be sure to record secret access keys because they will only be displayed once.

I created a video that shows the process of creating buckets and setting IAM permissions here: www.youtube.com/watch?v=FLIp6BLtwj...

Access to Access Key and Secret Access Key should be placed in your .env file.

Next, click the IAM user and add an inline policy. Edit it using the JSON editor, and then add the following (from the Flysystem document):

{"Version": "2012-10-17", "Statement": [{"Sid": "Stmt1420044805001", "Effect": "Allow", "Action": ["s3:ListBuckets", "s3:GetObject", "s3:GetObjectAcl", "s3:PutObject" "s3:PutObjectAcl", "s3:ReplicateObject", "s3:DeleteObject"], "Resource": ["arn:aws:s3:::my-awesome-bucket", "arn:aws:s3:::my-awesome-bucket/*"]}}

This enables us to perform the required S3 API operations on our new bucket.

Laravel usage

In Laravel, you can use file storage like this:

# if you set S3 to the default value: $contents = Storage::get ('path/to/file.ext'); Storage::put (' path/to/file.ext', 'some-content'); # if you don't set S3 as the default: $contents = Storage::disk (' S3')-> get ('path/to/file.ext'); Storage::disk (' S3')-> put ('path/to/file.ext',' some-content')

The path to the file (in S3) is appended to the bucket name, so the file named path/to/file.ext will exist in s3://my-awesome-bucket/path/to/file.ext `.

Technically, there are no directories in S3. In S3, the file is called "object" and the file path + name is "object key". So, in the bucket my-awesome-bucket, we just created an object with the key path/to/file.ext.

Useful information about S3

If your AWS setup has a server in the private network and uses a NAT gateway, be sure to create an S3 endpoint (gateway type). This is done in the Endpoints section of the VPC service. This allows calls to / from S3 to bypass the NAT gateway, thereby avoiding additional bandwidth charges. There is no extra cost to use it.

If you are worried about files being overwritten or deleted, consider enabling versioning in your S3 bucket

Consider enabling Intelligent Tiering in your S3 bucket to help save storage costs for files that you may not interact with again after old files

Please note that deleting large buckets (a large number of files) can cost money! This is because you have to make a large number of API calls to delete files.

These are all the contents of the article "how to use AWS S3 in Laravel". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report