All Projects → kszitt → next-oss

kszitt / next-oss

Licence: other
webpack打包文件上传到OSS

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to next-oss

Waliyun
阿里云Node.js Open API SDK(完整版)
Stars: ✭ 40 (+122.22%)
Mutual labels:  oss, cdn
Aliyun Sdk Js
阿里云 SDK for Javascript,支持在浏览器和 Nodejs 环境使用,支持大部分阿里云服务。
Stars: ✭ 727 (+3938.89%)
Mutual labels:  oss, cdn
ElUploader-OSS-Solution
ElementUI - Upload 组件结合 OSS 的封装
Stars: ✭ 28 (+55.56%)
Mutual labels:  oss, cdn
Ueditor Plus
Create By Xie Xianbin. RePackage ueditor for spring and other object storage service.
Stars: ✭ 71 (+294.44%)
Mutual labels:  oss, cdn
android-oss-best-practices
Best practices on creating Android OSS library projects [JA]
Stars: ✭ 32 (+77.78%)
Mutual labels:  oss
qt-creator-plugin-boostbuild
Boost.Build Project Manager Plugin for Qt Creator
Stars: ✭ 15 (-16.67%)
Mutual labels:  build
BuildTimeLogger-for-Xcode
A console app for logging Xcode build times and presenting them in a notification
Stars: ✭ 43 (+138.89%)
Mutual labels:  build
terraform-provider-graylog
Terraform Provider for Graylog
Stars: ✭ 21 (+16.67%)
Mutual labels:  oss
android manifest
The beginnings
Stars: ✭ 26 (+44.44%)
Mutual labels:  build
babel-preset-amex
✨ Standard babel preset for American Express
Stars: ✭ 14 (-22.22%)
Mutual labels:  build
Examples
An complete examples and related support for various popular projects, and more.
Stars: ✭ 22 (+22.22%)
Mutual labels:  build
aliyun-oss-laravel
Laravel 的 Aliyun OSS 扩展, 支持 Laravel 9. Alibaba Cloud Object Storage Service For Laravel.
Stars: ✭ 91 (+405.56%)
Mutual labels:  oss
jsdoc-action
📖 GitHub Action to build JSDoc documentation
Stars: ✭ 61 (+238.89%)
Mutual labels:  build
article
It’s Your Life. Share it. Celebrate it. Build it. AND HAVE FUN!
Stars: ✭ 33 (+83.33%)
Mutual labels:  build
workbox-cdn
Workbox Unofficial CDN and standalone NPM package.
Stars: ✭ 27 (+50%)
Mutual labels:  cdn
ukor
A Roku build tool with support for build flavors
Stars: ✭ 45 (+150%)
Mutual labels:  build
CDN-Ansible
Welcome to github repository for Visual Cloud Delivery Network. This repository will provide the Ansible playbooks for installing the components of Visual Cloud Delivery Network.
Stars: ✭ 20 (+11.11%)
Mutual labels:  cdn
yii2-aliyun-oss
Yii2 Aliyun OSS Yii2 阿里云 OSS
Stars: ✭ 41 (+127.78%)
Mutual labels:  oss
mos-tls-tunnel
Archived. Check this out https://github.com/IrineSistiana/simple-tls
Stars: ✭ 21 (+16.67%)
Mutual labels:  cdn
windows-azure-storage
Use the Microsoft Azure Storage service to host your website's media files.
Stars: ✭ 48 (+166.67%)
Mutual labels:  cdn

声明

目前,next-oss包已经不在维护。请webpack项目使用hsuc; next项目请使用next-hsuc

描述

将webpack打包生成的文件上传到OSS,以提高加载速度
目前,支持阿里云、华为云、七牛和又拍云。

安装

npm install next-oss --save-dev

要求

Node

Node.js >= 10.10.0 required

next框架

添加命令

// package.json
{
  "OSSDomainName": "<云端域名>",
  "OSSFolder": "<云端文件夹>",
  "OSSProduction": <:boolean>, // 生产模式是否使用OSS
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack",  // cross-env 请自行安装
  }
}

webpack配置文件

// webpack.config.js
const NextOss = require('next-oss');

...
plugins: [
  ...
  new NextOss({
    disable: process.env.NODE_ENV !== "production",
    upyun: {
      serviceName: "<service name>",
      operatorName: "<operator name>",
      operatorPassword: "<operator password>",
    },
    qiniu: {
      accessKey: "<ACCESS_KEY>",
      secretKey: "<SECRET_KEY>",
      bucket: "<Bucket>"
    },
    huawei: {
      accessKeyId: "<Provide your Access Key>",
      secretAccessKey: "<Provide your Secret Key>",
      server: "<https://your-endpoint>",
      bucket: "<Bucket>"
    },
    aliyun: {
      region: "<OSS region>",
      accessKeyId: "<Your accessKeyId>",
      accessKeySecret: "<Your accessKeySecret>",
      bucket: "<Your bucket name>"
    }
  })
]

部署

npm run build
// 将打包文件夹下的index.html文件部署到服务器,确保能访问到

next框架

添加命令

// package.json
{
  "OSSDomainName": "<云端域名>",
  "OSSFolder": "<云端文件夹>",
  "OSSProduction": <:boolean>, // 生产模式是否使用OSS
  "scripts": {
    "build": "cross-env NODE_ENV=production next build",  // 打包命令(cross-env 请自行安装)
    "start": "cross-env NODE_ENV=production node server.js"   // 启动服务(这里不能用next start,得自定义服务端)
  }
}

自定义服务端

// server.js
const express = require('express');
const next = require('next');
const {OSSFolder, OSSDomainName, OSSProduction} = require('./package.json');
const { NODE_ENV, PORT=3000 } = process.env;
const dev = NODE_ENV !== 'production';
const Prod = NODE_ENV === "production";
const app = next({dir: '.', dev});

// 是否使用OSS(只有生产模式可以使用OSS,开发模式无效)
if(Prod ? OSSProduction : false){
  app.setAssetPrefix(`${OSSDomainName}/${OSSFolder}/`);
}

app.prepare().then(() => {
    const server = express();

    server.listen(PORT, (err) => {
      if (err) {
        throw err
      }
      console.log(`> Ready on port ${PORT} [${NODE_ENV}]`);
    })
  }).catch((ex) => {
    console.log('An error occurred, unable to start the server')
    console.log(ex)
  });

webpack配置

// next.config.js
const NextOss = require('next-oss');
const {OSSFolder, OSSDomainName, OSSProduction} = require('./package.json');
const withPlugins = require ("next-compose-plugins");
const { NODE_ENV } = process.env;
const Prod = NODE_ENV === "production";

...
const nextConfig = {
  webpack: (config, options) => {
    ...
    let NextOssOptions = {
      disable: Prod ? !OSSProduction : true,  // 只有生产模式可以使用OSS,开发模式无效
      upyun: {
        serviceName: "<service name>",
        operatorName: "<operator name>",
        operatorPassword: "<operator password>",
      },
      qiniu: {
        accessKey: "<ACCESS_KEY>",
        secretKey: "<SECRET_KEY>",
        bucket: "<Bucket>"
      },
      huawei: {
        accessKeyId: "<Provide your Access Key>",
        secretAccessKey: "<Provide your Secret Key>",
        server: "<https://your-endpoint>",
        bucket: "<Bucket>"
      },
      aliyun: {
        region: "<OSS region>",
        accessKeyId: "<Your accessKeyId>",
        accessKeySecret: "<Your accessKeySecret>",
        bucket: "<Your bucket name>"
      }
    };
    if(!NextOssOptions.disable) options.config.assetPrefix = `${OSSDomainName}/"${OSSFolder}/`;
    config.plugins.push(
      new NextOss(NextOssOptions)
    );

    return config;
  }
};

module.exports = withPlugins([...], nextConfig);

部署

npm run build
npm run start

NextOSS(options)支持的选项

  • aliyun - 初始化阿里云OSS。
  • huawei - 初始化华为云OBS。
  • qiniu - 初始化七牛。
  • upyun - 初始化又拍云。
  • disable - 是否禁用,默认false
  • deletePrevBuildFile - 是否删除云端以前的版本,默认false
  • log - 是否显示日志,默认true
  • cover - 图片、字体文件是否覆盖,默认true

对象存储CORS规则设置

注意事项

  • 云端访问权限请设置为“公共读写”或者“公共读”
  • options参数中aliyunhuaweiqiniuupyun同时配置只有第一个有效
  • options.disable 该插件在非生产模式禁用,生产模式可以在package.json中的OSSProduction设置是否禁用。
  • options.deletePrevBuildFile 启用该项会把以前的版本删掉,建议在服务器定期清理。
  • options.cover 设置为false不覆盖时,请将图片、字体的文件名添加[hash]值。否则,会找不到资源
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].