{"id":2236,"date":"2024-11-10T17:24:37","date_gmt":"2024-11-10T09:24:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2236\/"},"modified":"2024-11-10T17:24:37","modified_gmt":"2024-11-10T09:24:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-nodejsd-%e4%b8%ad%e9%9b%86%e6%88%90-cloudinary","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2236\/","title":{"rendered":"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173069701275997.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u63d2\u56fe\" \/><\/p>\n<p>\u9ad8\u6548\u7ba1\u7406\u56fe\u50cf\u548c\u89c6\u9891\u7b49\u5a92\u4f53\u8d44\u4ea7\u5bf9\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\uff0ccloudinary \u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fa\u8272\u7684\u89e3\u51b3\u65b9\u6848\u6765\u65e0\u7f1d\u5904\u7406\u8fd9\u4e9b\u8d44\u4ea7\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd cloudinary \u5728 node.js \u9879\u76ee\u4e2d\u7684\u96c6\u6210\u8fc7\u7a0b\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u4e91\uff1f <\/h2>\n<p>cloudinary \u662f\u4e00\u79cd\u57fa\u4e8e\u4e91\u7684\u5a92\u4f53\u7ba1\u7406\u670d\u52a1\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ee5 web \u53cb\u597d\u7684\u683c\u5f0f\u8f7b\u677e\u5b58\u50a8\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u56fe\u50cf\u548c\u89c6\u9891\u3002\u51ed\u501f\u81ea\u52a8\u56fe\u50cf\u4f18\u5316\u3001\u54cd\u5e94\u5f0f\u8f6c\u6362\u548c\u901a\u8fc7 cdn \u8fdb\u884c\u5185\u5bb9\u4ea4\u4ed8\u7b49\u529f\u80fd\uff0ccloudinary \u5df2\u6210\u4e3a\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u3002<br \/> \u63a2\u7d22 cloudinary \u5b9a\u4ef7<\/p>\n<h2> \u5148\u51b3\u6761\u4ef6 <\/h2>\n<p>\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\uff1a<\/p>\n<ul>\n<li>\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 node.js<\/li>\n<li>\u57fa\u672c\u7684 node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e<\/li>\n<li>\u4e00\u4e2acloudinary\u5e10\u6237\uff08\u5982\u679c\u60a8\u6ca1\u6709\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u6ce8\u518c\uff09<\/li>\n<\/ul>\n<h2> \u7b2c1\u6b65\uff1a\u5b89\u88c5cloudinary sdk <\/h2>\n<pre>npm install cloudinary\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c2\u6b65\uff1a\u914d\u7f6ecloudinary <\/h2>\n<pre>const cloudinary = require('cloudinary').v2;\n\ncloudinary.config({\n  cloud_name: 'your_cloud_name',\n  api_key: 'your_api_key',\n  api_secret: 'your_api_secret',\n});\n\nmodule.exports = cloudinary;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c\u4e09\u6b65\uff1a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf <\/h2>\n<pre>cloud_name=your-cloud-name\napi_key=your-api-key\napi_secret=your-api-secret\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>npm install dotenv\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>require('dotenv').config();\n\ncloudinary.config({\n  cloud_name: process.env.cloud_name,\n  api_key: process.env.api_key,\n  api_secret: process.env.api_secret,\n});\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c4\u6b65\uff1a\u5c06\u56fe\u50cf\u4e0a\u4f20\u5230cloudinary <\/h2>\n<pre>const cloudinary = require('.\/config');\n\nasync function uploadimage(imagepath) {\n  try {\n    const result = await cloudinary.uploader.upload(imagepath, {\n      folder: 'samples', \/\/ optional: specify the folder to store images\n    });\n    console.log('image uploaded successfully:', result.url);\n    return result.url;\n  } catch (error) {\n    console.error('error uploading image:', error);\n  }\n}\n\n\/\/ example usage\nuploadimage('path\/to\/your\/image.jpg');\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c 5 \u6b65\uff1a\u4f7f\u7528 cloudinary \u8f6c\u6362\u56fe\u50cf <\/h2>\n<pre>const transformedimageurl = cloudinary.url('sample.jpg', {\n  width: 400,\n  height: 300,\n  crop: 'fill',\n});\n\nconsole.log('transformed image url:', transformedimageurl);\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c 6 \u6b65\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406\u6587\u4ef6\u4e0a\u4f20 <\/h2>\n<pre>npm install multer\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    <\/p>\n<pre>const multer = require('multer');\nconst upload = multer({ dest: 'uploads\/' }); \/\/ temporary folder for uploaded files\n\napp.post('\/upload', upload.single('image'), async (req, res) =&gt; {\n  try {\n    const imagepath = req.file.path;\n    const imageurl = await uploadimage(imagepath);\n    res.json({ imageurl });\n  } catch (error) {\n    res.status(500).json({ error: 'failed to upload image' });\n  }\n});\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7b2c 7 \u6b65\uff1a\u4f18\u5316\u548c\u4ea4\u4ed8\u5a92\u4f53\u8d44\u4ea7 <\/h2>\n<pre>const optimizedImageUrl = cloudinary.url('sample.jpg', {\n  fetch_format: 'auto',\n  quality: 'auto',\n});\n\nconsole.log('Optimized Image URL:', optimizedImageUrl);\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5c06 cloudinary \u96c6\u6210\u5230\u60a8\u7684 node.js \u9879\u76ee\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u5e76\u4e14\u6253\u5f00\u4e86\u5a92\u4f53\u7ba1\u7406\u529f\u80fd\u7684\u4e16\u754c\u3002\u65e0\u8bba\u60a8\u5904\u7406\u56fe\u50cf\u8fd8\u662f\u89c6\u9891\uff0ccloudinary \u5f3a\u5927\u7684 api \u90fd\u53ef\u4ee5\u8ba9\u60a8\u8f7b\u677e\u9ad8\u6548\u5730\u4f18\u5316\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u8d44\u4ea7\u3002<\/p>\n<p>\u7f16\u7801\u6109\u5feb\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Nodejsd \u4e2d\u96c6\u6210 Cloudinary\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u6548\u7ba1\u7406\u56fe\u50cf\u548c\u89c6\u9891\u7b49\u5a92\u4f53\u8d44\u4ea7\u5bf9\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\uff0ccloudinary \u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fa\u8272\u7684\u89e3\u51b3\u65b9\u6848\u6765\u65e0\u7f1d\u5904\u7406\u8fd9\u4e9b\u8d44\u4ea7\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd cloudinary \u5728 node.js \u9879\u76ee\u4e2d\u7684\u96c6\u6210\u8fc7\u7a0b\u3002 \u4ec0\u4e48\u662f\u4e91\uff1f cloudinary \u662f\u4e00\u79cd\u57fa\u4e8e\u4e91\u7684\u5a92\u4f53\u7ba1\u7406\u670d\u52a1\uff0c\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ee5 web \u53cb\u597d\u7684\u683c\u5f0f\u8f7b\u677e\u5b58\u50a8\u3001\u8f6c\u6362\u548c\u4ea4\u4ed8\u56fe\u50cf\u548c\u89c6\u9891\u3002\u51ed\u501f\u81ea\u52a8\u56fe\u50cf\u4f18\u5316\u3001\u54cd\u5e94\u5f0f\u8f6c\u6362\u548c\u901a\u8fc7 cdn \u8fdb\u884c\u5185\u5bb9\u4ea4\u4ed8\u7b49\u529f\u80fd\uff0ccloudinary \u5df2\u6210\u4e3a\u8bb8\u591a\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u3002 \u63a2\u7d22 cloudinary \u5b9a\u4ef7 \u5148\u51b3\u6761\u4ef6 \u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\uff1a \u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 node.js \u57fa\u672c\u7684 node.js \u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e \u4e00\u4e2acloudinary\u5e10\u6237\uff08\u5982\u679c\u60a8\u6ca1\u6709\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u6ce8\u518c\uff09 \u7b2c1\u6b65\uff1a\u5b89\u88c5cloudinary sdk npm install cloudinary \u767b\u5f55\u540e\u590d\u5236 \u7b2c2\u6b65\uff1a\u914d\u7f6ecloudinary const cloudinary = require(&#8216;cloudinary&#8217;).v2; cloudinary.config({ cloud_name: &#8216;your_cloud_name&#8217;, api_key: &#8216;your_api_key&#8217;, api_secret: &#8216;your_api_secret&#8217;, }); module.exports = cloudinary; \u767b\u5f55\u540e\u590d\u5236 \u7b2c\u4e09\u6b65\uff1a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf cloud_name=your-cloud-name api_key=your-api-key api_secret=your-api-secret \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2236","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2236"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2236\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}