我对AWS很陌生,我在那个区域创建了s3 bucket,并将access keys区域传递到我的.env文件中,并编辑了conig/filesystems.php和我编写的用于发布数据的控制器,我的数据是混合型的,它包含二进制和普通数据,所以我有点困惑在postman中必须使用哪种内容类型(content-type=,在头内)。如果我使用普通content-type=application/json,在body部分我使用raw,当时它可以工作,但图像没有上传到s3 bucket中,当我使用content-type=multipart/mixed时,我得到以下错误,请帮助我解决这个问题,以及如何通过邮递员从laravel上传图像到s3 bucket
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null (SQL: insert into `books` (`image`, `price`, `title`, `quantity`, `author`, `description`, `user_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, 1, 2021-07-01 08:09:06, 2021-07-01 08:09:06)) in file C:\Users\VICKY\Desktop\8\laravel-bookstore\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 692
books.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Books extends Model
{
use HasFactory;
protected $fillable = [
// 'name',
'image',
'price',
'title',
'quantity',
// 'ratings',
'author' ,
'description'
];
protected $hidden = [
'password',
'remember_token',
'updated_at'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
//inverse one to many
public function user(){
return $this->belongsTo(User::class);
}
}
bookscontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Books;
use App\Models\User;
use App\Http\Requests;
use Symfony\Component\HttpFoundation\Response;
use App\Http\Resources\Books as BooksResource;
use App\Http\Middleware\Authenticate;
class BooksController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function DisplayBooks()
{
$books=Books::all();
return User::find($books->user_id=auth()->id())->books;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function AddBooks(Request $request)
{
$book=new Books();
//$book->name=$request->input('name');
$book->image=$request->input('image');
if($request->hasfile('image'))
{
$file = $request->file('image');
$imageName=time().$file->getClientOriginalName();
$filePath = 'images/' . $imageName;
Storage::disk('s3')->put($filePath, file_get_contents($file));
}
$book->price=$request->input('price');
$book->title=$request->input('title');
$book->quantity=$request->input('quantity');
// $book->ratings=$request->input('ratings');
$book->author=$request->input('author');
$book->description=$request->input('description');
$book->user_id = auth()->id();
$book->save();
return new BooksResource($book);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function ShowBook($id)
{
$book=Books::findOrFail($id);
if($book->user_id==auth()->id())
return new BooksResource($book);
else{
return response()->json([
'error' => 'UnAuthorized/invalid id'], 401);
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function UpdateBook(Request $request, $id)
{
$book=Books::findOrFail($id);
if($book->user_id==auth()->id()){
//$book->name=$request->input('name');
$book->image=$request->input('image');
$book->price=$request->input('price');
$book->title=$request->input('title');
$book->quantity=$request->input('quantity');
//$book->ratings=$request->input('ratings');
$book->author=$request->input('author');
$book->description=$request->input('description');
$book->save();
return new BooksResource($book);
}
else
{
return response()->json([
'error' => ' Book is not available ith id'], 404);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function DeleteBook($id)
{
$book=Books::findOrFail($id);
if($book->user_id==auth()->id()){
if($book->delete()){
return response()->json(['message'=>'Deleted'],201);
}
}
else{
return response()->json([
'error' => ' Method Not Allowed/invalid Book id'], 405);
}
}
}
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'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'),
'visibility'=>'public',
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
迁移表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('image');
$table->integer('price')->unsigned();
$table->text('title');
$table->integer('quantity')->length(2)->unsigned();
// $table->integer('ratings')->length(2)->unsigned();
$table->string('author');
$table->longText('description');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
为了在laravel中以二进制形式简单地将图像存储在数据库中,请参阅以下内容:如何使用laravel从数据库中存储和检索图像内容
存储在AWS中
编写器需要连接/flysystem-aws-s3-v3
AWS_ACCESS_KEY_ID=Access Key Id
AWS_SECRET_ACCESS_KEY=Secret Access Key
AWS_DEFAULT_REGION=Bucket Region
AWS_BUCKET=Bucket Name
if($request->hasfile('image'))
{
$file = $request->file('image');
$imageName=time().$file->getClientOriginalName();
$filePath = 'images/' . $imageName;
Storage::disk('s3')->put($filePath, file_get_contents($file));
// After Image is uploaded make entry to database
}
我想知道如何上传一个图像通过API邮递员在拉威尔和保存到我的数据库,这是phpMyAdmin。当我尝试在postman中运行代码时,会显示以下内容: 这是我目前的代码: 横幅积垢控制器: 横幅型号: 任何关于我做错了什么的想法,我想现在就得到这个,因为我有很多表格需要创建。 希望有人能帮助我开始。谢谢。
我的主要目标是使用Microsoft Graph API将。pptx/.docx/.pdf文件上传到Microsoft Sharepoint。我可以使用下面的“PUT”请求上传到简单的文本文件,其中的内容类型是多部分/表单数据 ---------------------------------404518839734975569926100内容-处理:表单-数据;name=“文件”;filenam
我试图上传一个文件到我的服务器使用一个endpoint通过Spring公开。然而,当我试图通过邮递员测试api时,我得到当前请求不是一个多部分请求错误。我通过这个问题多部分异常:当前请求不是多部分请求,但仍然无法修复此问题。请帮助。提前感谢。 这是我的控制器: 我的服务: 正如您在下面看到的,我将文件作为表单数据发送,并且没有设置任何标题
我正试图上传邮递员表单数据的图像。我无法得到输入 请帮忙提前感谢。
我需要上传一个文件到S3与邮递员测试。我有一个从S3生成的预签名URL。 但是我不知道如何正确地配置postman,以便对生成的预签名URL执行此PUT请求。 邮件头格式错误的postman上的响应是: 预签名请求的参数: 我得到了URL,然后在邮递员中输入它,用PUT请求,但标题不完整?1.如果我在头上添加content-type,服务器2没有响应。在请求的正文中,Im以“二进制”的形式附加文件
2FE552B7-53B1-4E4A-AFFF-3AEF8FE9D05B-----WebKitFormBoundaryLH8FJWGYEEVTCJMA内容-配置:表单-数据;name=“sample test file.pdf”;filename=“sample test file.pdf”content-type:application/pdf