上传文件到阿里云oss

先用composer切换到项目根目录执行

composer require aliyuncs/oss-sdk-php

安装好了后如图在这里插入图片描述
模块创建aliyunoss,并创建相应的控制器视图文件夹,在aliyunoss文件夹下创建common配置文件

<?php
namespace app\aliyunoss\controller;

use think\Controller;
use think\Config;
use OSS\OssClient;
use OSS\Core\OssException;
class common extends Controller
{
    Public function moveOss($accessKeyId,$accessKeySecret,$endpoint,$bucket,$object,$content)
    {
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $res= $ossClient->putObject($bucket, $object, $content);
        } catch (OssException $e) {
            print $e->getMessage();
        }
        return $res['info']['url'];
    }
}

创建视图:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="fileinfo" action="{:url('index/index')}">
    <table>
        <tr>
            <td>上传文件:</td>
            <td><input type="file" name="image[]" multiple="multiple"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="上传" ></td>
        </tr>
    </table>
</form>
</body>
</html>

控制器文件:

<?php
namespace app\aliyunoss\controller;

use think\Controller;
use think\File;
class Index extends common
{


    public function index()
    {
        error_reporting(0);
        header("Content-type:text/html;charset=utf-8");
        if($this->request->isPost()){
            $arrList1= $_FILES['image']['name'];
            $arrList2= $_FILES['image']['tmp_name'];
            $info2=array();
            for($i=0;$i<count($arrList1);$i++){
                $object= $arrList1[$i];
                $content=file_get_contents($arrList2[$i]);
                $info=$this->moveOss('你的LTAIwoPLgOMBFbjY','你的accessKeySecret','填写你的地址','你的bucket',$object,$content);
                $arr2[]=$info;
              //echo $info;echo "<br/>";
            }
           $result=implode(';',$arr2);
           print_r($result);

        }else{
            return view();
        }

    }
  
}

上传成功后返回地址:
在这里插入图片描述

上传文件阿里云OSS可以通过以下步骤实现: 1. 引入aliyun-java-sdk-oss依赖 ``` <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-java-sdk-oss</artifactId> <version>3.10.0</version> </dependency> ``` 2. 创建OSSClient对象 ``` String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; String accessKeyId = "yourAccessKeyId"; String accessKeySecret = "yourAccessKeySecret"; String bucketName = "yourBucketName"; OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); ``` 3. 上传文件 ``` String objectName = "yourObjectName"; // 上传OSS后的文件名,可以包含路径 File file = new File("yourLocalFile"); // 要上传的文件路径 ossClient.putObject(bucketName, objectName, file); ``` 4. 关闭OSSClient对象 ``` ossClient.shutdown(); ``` 完整的Java代码示例: ``` import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import java.io.File; public class OSSUploader { public static void main(String[] args) { String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; String accessKeyId = "yourAccessKeyId"; String accessKeySecret = "yourAccessKeySecret"; String bucketName = "yourBucketName"; String objectName = "yourObjectName"; // 上传OSS后的文件名,可以包含路径 File file = new File("yourLocalFile"); // 要上传的文件路径 // 创建OSSClient对象 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件 ossClient.putObject(bucketName, objectName, file); // 关闭OSSClient对象 ossClient.shutdown(); } } ``` 需要注意的是,上传文件时可以指定上传后的文件名,也可以使用原文件名。同时,上传文件的大小不能超过OSS的限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值